0

I want to update an existing object/image in a Google Slide. This works as long as the object exists:

var requests = [
    {
      "deleteObject": {
        "objectId": 'image01'
      }
    },
    {
      "createImage": {
        "url": imageUrl,
        "objectId": 'image01',
        "elementProperties": {
          "pageObjectId": pageId,
          "size": {
            "width": {
              "magnitude": 250,
              "unit": "PT"
            },
            "height": {
              "magnitude": 250,
              "unit": "PT"
            }
          },
          "transform": {
            "scaleX": 1,
            "scaleY": 1,
            "translateX": 200,
            "translateY": 100,
            "unit": "PT"
          }
        }
      }
    }
  ];
  var response = Slides.Presentations.batchUpdate({'requests': requests}, presentationId);

However, if a user previously deleted the object in the presentation, it is not re-created.

The following error message appear:

Invalid requests[0].deleteObject: The object (image01) could not be found.

How can I query whether an object exists in presentation?

Roberto B
  • 111
  • 2
  • 10
  • Why does the error refer to the object as MyImage_01 when there is no mention of that string in the var requests? Are you using the wrong name perhaps? – Cooper Sep 25 '17 at 15:27
  • Thank you. That was my fault. The error message with MyImage_01 was from another function. I have corrected it. – Roberto B Sep 26 '17 at 04:25

1 Answers1

0

How about retrieving a object list using slides.presentations.get? In order to confirm whether objects exist, it uses slides/pageElements/objectId for fields of slides.presentations.get. You can know the exist of objects using the object list.

Sample script :

var response = Slides.Presentations.get(presentationId);
response.slides.forEach(function(e1, i1){
  e1.pageElements.forEach(function(e2){
    Logger.log("Page %s, objectId %s", i1 + 1, e2.objectId);
  });
});

Result :

Page 1.0, objectId ###
Page 2.0, objectId ###
Page 3.0, objectId ###

If this was not useful for you, I'm sorry.

Edit :

If you want to search a value from whole JSON, you can use following simple script. When value2 is included in sampledata, ~JSON.stringify(sampledata).indexOf('value2') becomes true. In this sample, ok is shown, because value2 is included in sampledata.

But it's a bit of a stretch. If you can know the complete structure of JSON, I think that the compare of value using key is better.

var sampledata = {key1: "value1", key2: "value2"};
if (~JSON.stringify(sampledata).indexOf('value2')) { 
  Logger.log("ok")
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you, this was useful. The variable 'response' contains all information I need. How can I search for the string 'image01' in the variable? response.indexOf('image01'); does not work – Roberto B Sep 26 '17 at 05:01
  • @Roberto B What is the string 'image01'? ``objectId`` is the string like ``g1234567890_0_0``. If you had modified the objectId to 'image01', you can use it. But I don't know how to modify the objectId. If you want to update the object, after it retrieved objectId, you can update it using objectId. I worry about I may misunderstand your comment. I'm sorry for the inconvenience. – Tanaike Sep 26 '17 at 05:38
  • The variable 'response' contains the string 'image01'. I found it with Logger.log. Now I have to query it. If the string exists, it should output the value true. If not, then the value is false. In an array I find it with indexOf. But how can I find a string in the variable 'response'? Or should I fill an extra array in your function? f.e. var response = Slides.Presentations.get(presentationId); response.slides.forEach(function(e1, i1){ e1.pageElements.forEach(function(e2){ Logger.log("Page %s, objectId %s", i1 + 1, e2.objectId); array.push(objectId); }); }); – Roberto B Sep 26 '17 at 06:41
  • @Roberto B I have 2 questions for your comment. 1. Can you show us the result included 'image01' in the response? In my environment, my Logger.log() is not included 'image01'. If I could know it, I might be able to propose a sample script. 2. Can you show us about the method to create the object with objectId of 'image01'? If I could create the same scenes, I can think of the solution. I'm sorry for the inconvenience. – Tanaike Sep 26 '17 at 07:01
  • content of response: `......EFT}}}, transform={scaleX=217.7858, scaleY=217.7858, unit=EMU, translateY=1363157.895, translateX=2000000}, size={width={unit=EMU, magnitude=27550}, height={unit=EMU, magnitude=15950}}, **objectId=image01**}], pageProperties={pageBackgroundFill={propertyState=INHERIT}}, slideProperties={notesPage={pageElements=[{transform={scaleX=2.032, scaleY=1.143, unit=EMU, translateY=685800, translateX=381300}, shape={shapePropertie........` – Roberto B Sep 26 '17 at 07:10
  • creating the object: var imageId = 'image01'; `createImage: { objectId: imageId, url: imageUrl, elementProperties: { pageObjectId: pageId, size: { height: emu4M, width: emu4M }, transform: { scaleX: 1, scaleY: 1, translateX: 2000000, translateY: 100000, unit: 'EMU' } } }` – Roberto B Sep 26 '17 at 07:11
  • @Roberto B Using the key which has ``image01**``, you can compare whether "image01" is included in the value using a script like ``if (~value.indexOf('image01')) { do something }``. Although I cannot retrieve the complete key from a part of JSON data you showed, you who has the complete JSON can do it. And about creating the object, I didn't know what you did using that. I'm sorry. – Tanaike Sep 26 '17 at 07:36
  • This is my problem. I can't search in the variable 'response'. If I use `if (response.indexOf('image01')) { Logger.log('hello world'); }` the error message appears `TypeError: Funktion indexOf in Objekt {"slides":[{"pageElements":[{"transform":` – Roberto B Sep 26 '17 at 07:47
  • @Roberto B OK. I could understand your problem. I can prepare a sample script for searching value. Please wait a moment. – Tanaike Sep 26 '17 at 07:52
  • Yeah, that's it. Thank you for your patience! – Roberto B Sep 26 '17 at 08:07
  • @Roberto B Thank you, too. But I'm sorry for being late. – Tanaike Sep 26 '17 at 08:08