Google 使用来自驱动器的图像的脚本 replaceAllShapesWithImage 不再起作用

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

从昨天开始,我的一个 google 脚本就不再起作用了。 剧本

  1. 在驱动器上拍摄图像
  2. 复制一张幻灯片
  3. 用图像替换形状

但是我得到了这个错误:

"The provided image is in an unsupported format."

-> 我授予对图像的所有访问权限:它不会改变任何东西

-> 如果我将 url 带出驱动器

,脚本就会工作

任何想法

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


}

这个答案怎么样?请将此视为几个可能的答案之一。

问题和解决方法:

我认为你的问题的原因是由于对 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.

根据以上情况,无法使用var imageUrl = file.getDownloadUrl() + "&access_token=" + ScriptApp.getOAuthToken();的URL。例如,访问URL时,即使使用了access token,也会显示登录画面。

为了避免这个问题,下面的修改怎么样?

修改点:

  • 文件已公开共享并放入 Google 幻灯片。然后,共享文件关闭。
    • 在这种情况下,即使关闭文件共享,幻灯片上的图片也不会被删除。
  • webContentLink 用作 URL。
    • 就像https://drive.google.com/uc?export=download&id=###

修改后的脚本:

当你的脚本修改后,变成如下。

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
}

参考文献:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。