Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

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

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);


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...