温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Google script replaceAllShapesWithImage with image from drive doesn"t work any more
google-apps-script google-drive-api google-slides-api google-slides

其他 - Google脚本用驱动器中的图像替换allShapesWithImage不再起作用

发布于 2020-04-21 11:40:55

从昨天开始,我的Google脚本之一不再起作用。剧本

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

但是我得到了这个错误:

“提供的图像格式不受支持。”

->我拥有对图像的所有访问权限:它没有任何改变

->如果我在驱动器外添加一个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);


}

查看更多

提问者
Denis Bolomier
被浏览
51
4,904 2020-02-10 22:13

这个答案怎么样?请认为这只是几个可能的答案之一。

问题和解决方法:

我认为您提出该问题的原因是由于对官方文件进行了以下修改

首先,我们正在更改Google Drive API的授权。如果您使用查询参数中的访问令牌授权对Drive API的下载请求,则需要迁移请求以使用HTTP标头进行身份验证。从2020年1月1日开始,将不再支持对使用query参数中的访问令牌进行身份验证的files.get,versions.get和files.export端点的下载调用,这意味着您需要更新身份验证方法。

在上述情况下,var imageUrl = file.getDownloadUrl() + "&access_token=" + ScriptApp.getOAuthToken();无法使用的URL 例如,当访问URL时,即使使用访问令牌,也会显示登录屏幕。

为了避免此问题,如何进行以下修改?

修改要点:

  • 该文件已公开共享,并已放入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
}

参考文献:

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