Google Slides API Adding and Cropping Images

421 views
Skip to first unread message

Clay Smith

unread,
Jul 19, 2019, 7:03:16 AM7/19/19
to Google Apps Script Community

I'm struggling to get the API to update the Offset of an image. It's returned that the ObjectID could not be found. When I use Google Apps Script. I've had a 200 response when using the API Explorer but didn't see any visual change. When I do the batch update creating the image and then updating it I get a 200 response but the image never displays.  What am I missing here?


var data = {requests=[{updateImageProperties={imageProperties={cropProperties={topOffset=0.2, bottomOffset=0.2, rightOffset=0.2, leftOffset=0.2, angle=0.0}}, fields=cropProperties.leftOffset, cropProperties.rightOffset, cropProperties.topOffset, cropProperties.bottomOffset, objectId=SLIDES_API372930775_0}}]}

var response = {
  "error": {
    "code": 400,
    "message": "Invalid requests[0].updateImageProperties: The object (SLIDES_API372930775_0) could not be found.",
    "status": "INVALID_ARGUMENT"
  }
}

Two different Code snippet methods below.

          
var p =  SlidesApp.getActivePresentation().getSlideById(slide_id).insertImage(url);
          var objID = p.getObjectId();

          var data = {"requests": [
            {"updateImageProperties": {
                "imageProperties": {
                  "cropProperties": {
                    "leftOffset": .2,
                    "rightOffset": .2,
                    "topOffset": .2,
                    "bottomOffset": .2,
                    "angle": 0
                  }
                },
               "fields": "cropProperties.leftOffset, cropProperties.rightOffset, cropProperties.topOffset, cropProperties.bottomOffset",
              "objectId": objID.toString(),
              }
            }
          ]};
          var options = {
            'headers':{'Authorization':'Bearer ' + ScriptApp.getOAuthToken()},
            'method' : 'post',
            'muteHttpExceptions': true,
            'contentType': 'application/json',
            'payload' : JSON.stringify(data) 
          };
          Logger.log(data);
          var response = UrlFetchApp.fetch('https://slides.googleapis.com/v1/presentations/'+ SlidesApp.getActivePresentation().getId() +':batchUpdate',options);



function addImage(presentationId, pageId, url, h, w, t, l) {
  var pageElementId = Utilities.getUuid();
  var requests = [{
    'createImage': {
      'objectId': pageElementId,
      'url': url,
      'elementProperties': {
        'pageObjectId': pageId,
        'size': {
          'width': {
            'magnitude': w,
            'unit': 'EMU'
          },
          'height': {
            'magnitude': h,
            'unit': 'EMU'
          }
        },
        'transform': {
          'scaleX': 1,
          'scaleY': 1,
          'translateX': l * 72 / 96,
          'translateY': t * 72 / 96,
          'unit': 'PT'
        }
      }
    }
  }, {
    "updateImageProperties": {
                "imageProperties": {
                  "cropProperties": {
                    "leftOffset": 0.5,
                    "rightOffset": 0.2,
                    "topOffset": 0.2,
                    "bottomOffset": 0.2,
                    "angle": 0
                  }
                },
               "fields": "cropProperties.leftOffset, cropProperties.rightOffset, cropProperties.topOffset, cropProperties.bottomOffset",
              "objectId": pageElementId
    }
  }];
  var response =Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
  Logger.log('Created Image with ID: ' + response);
}

Clark Lind

unread,
Jul 19, 2019, 8:24:13 AM7/19/19
to Google Apps Script Community
Out of my league here, but dumb question. In your addImage function, should the 'url' property be 'sourceUrl' instead? (Does that even matter?) That was the only thing I was able to see. (reference)

Clay Smith

unread,
Jul 19, 2019, 8:40:17 AM7/19/19
to google-apps-sc...@googlegroups.com
I believe that is part of the GET request of an image. I'm working with the create image.  https://developers.google.com/slides/reference/rest/v1/presentations/request#CreateImageRequest But could certainly been the area of issue. I'll explore with some other urls.  

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ff31e7b4-f14f-44d2-a2ac-3681f237f4b1%40googlegroups.com.

Clay Smith

unread,
Jul 19, 2019, 10:09:37 AM7/19/19
to google-apps-sc...@googlegroups.com
And all I can actually come up with is that the crop properties are read-only. 

"The crop properties of the image. If not set, the image is not cropped. This property is read-only."

Clay Smith

unread,
Jul 19, 2019, 11:32:11 AM7/19/19
to google-apps-sc...@googlegroups.com
Please make a request here  https://issuetracker.google.com/issues/113978819 to have the cropping editable from the API.

Thanks
Reply all
Reply to author
Forward
0 new messages