Google script replaceAllShapesWithImage with image from drive doesn"t work any more

扶醉桌前 提交于 2020-03-10 04:37:35

问题


Since yesterday one of my google script doesn't work anymore. The script

  1. take an image on the drive
  2. copie a slide
  3. replace a shape with an image

But I got this error:

"The provided image is in an unsupported format."

-> I give all access to the image: it doesn't change anything

-> The script work if I take an url outside the drive

Any idea

function test_image(){
  var imageUrls = DriveApp.getFilesByName("DSC_3632.png");
  var file = "undefined";
  while ( imageUrls.hasNext()) {
    var file = imageUrls.next();
  }

  var imageUrl = file.getDownloadUrl() + "&access_token=" + ScriptApp.getOAuthToken();

  var model_file = DriveApp.getFileById("your-id");
  var presentation = model_file.makeCopy("totot");
  var presentation =Slides.Presentations.get(presentation.getId())

  var requests = [{
      "replaceAllShapesWithImage":
        {
          "imageUrl": imageUrl,
          "imageReplaceMethod": "CENTER_INSIDE",
          "containsText": {
            "text": "toto",
            "matchCase": false,
          }
        }
    }];


  var presentationId = presentation.presentationId

  var createSlideResponse = Slides.Presentations.batchUpdate({
    requests: requests
  }, presentationId);


}

回答1:


How about this answer? Please think of this as just one of several possible answers.

Issue and workaround:

I think that the reason of your issue is due to the following modification of official document.

First, we’re making changes to authorization for the Google Drive API. If you authorize download requests to the Drive API using the access token in a query parameter, you will need to migrate your requests to authenticate using an HTTP header instead. Starting January 1, 2020, download calls to files.get, revisions.get and files.export endpoints which authenticate using the access token in the query parameter will no longer be supported, which means you’ll need to update your authentication method.

By above situation, the URL of var imageUrl = file.getDownloadUrl() + "&access_token=" + ScriptApp.getOAuthToken(); cannot be used. For example, when it accesses to the URL, the login screen is displayed even when the access token is used.

In order to avoid this issue, how about the following modification?

Modification points:

  • The file is shared publicly and put to Google Slides. Then, the sharing file is closed.
    • In this case, even when the share of file is closed, the put image on Slides is not removed.
  • The webContentLink is used as the URL.
    • It's like https://drive.google.com/uc?export=download&id=###.

Modified script:

When your script is modified, it becomes as follows.

function test_image(){
  var imageUrls = DriveApp.getFilesByName("DSC_3632.png");
  var file; // Modified
  while (imageUrls.hasNext()) {
    file = imageUrls.next();
  }
  file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW); // Added
  var imageUrl = "https://drive.google.com/uc?export=download&id=" + file.getId(); // Modified
  var model_file = DriveApp.getFileById("your-id");
  var presentation = model_file.makeCopy("totot");
  var presentation =Slides.Presentations.get(presentation.getId())
  var requests = [{
    "replaceAllShapesWithImage": {
      "imageUrl": imageUrl,
      "imageReplaceMethod": "CENTER_INSIDE",
      "containsText": {
        "text": "toto",
        "matchCase": false,
      }
    }
  }];
  var presentationId = presentation.presentationId
  var createSlideResponse = Slides.Presentations.batchUpdate({requests: requests}, presentationId);
  file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE); // Added
}

References:

  • Upcoming changes to the Google Drive API and Google Picker API
  • setSharing()

If I misunderstood your question and this was not the direction you want, I apologize.



来源:https://stackoverflow.com/questions/60076883/google-script-replaceallshapeswithimage-with-image-from-drive-doesnt-work-any-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!