gpt4 book ai didi

google-apps-script - 使用来自驱动器的图像的 Google 脚本 replaceAllShapesWithImage 不再有效

转载 作者:行者123 更新时间:2023-12-05 08:51:23 24 4
gpt4 key购买 nike

从昨天开始,我的一个谷歌脚本就不能用了。脚本

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


}

最佳答案

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

问题和解决方法:

我认为你的问题的原因是由于对 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 时,即使使用访问 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
}

引用资料:

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

关于google-apps-script - 使用来自驱动器的图像的 Google 脚本 replaceAllShapesWithImage 不再有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60076883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com