通过 DriveApp.getFileById 从 Google Drive 到 Google Slides 的图像

2024-03-18

我想在按照说明后通过 Google Apps 脚本将图像导入到 Google 幻灯片Google 开发者 - 如何... https://developers.google.com/slides/how-tos/add-image#about_images

function createImage(presentationId) {
// Add a new image to the presentation page. The image is assumed to exist in
// the user's Drive, and have 'imageFileId' as its file ID.
var requests = [];
var pageId = 'g1fe0c....';
var imageId = 'MyImage_01';
var imageUrl = DriveApp.getFileById('0B6cQESmLJfX...').getUrl();

var emu4M = {
  magnitude: 4000000,
  unit: 'EMU'
};
requests.push({
  createImage: {
    objectId: imageId,
    url: imageUrl,
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: emu4M,
        width: emu4M
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 100000,
        translateY: 100000,
        unit: 'EMU'
      }
    }
  }
});

// Execute the request.
var response = Slides.Presentations.batchUpdate({
  requests: requests
}, presentationId); 

}

但对于我尝试过的每种图像格式,Google Apps 脚本中都会出现错误消息:

无效的请求[0].createImage:提供的图像位于 不支持的格式。

Drive- 和 Slides API 在 Google 高级服务中激活。该文件夹和文件具有公共共享。

有人使用命令 DriveApp.getFileById() 并成功导出到 Google 幻灯片吗?


下面的修改怎么样?

From :

var imageUrl = DriveApp.getFileById('0B6cQESmLJfX...').getUrl();

To :

var imageUrl = DriveApp.getFileById('0B6cQESmLJfX...').getDownloadUrl() + "&access_token=" + ScriptApp.getOAuthToken();

从这个文档来看,似乎需要访问令牌。https://developers.google.com/slides/how-tos/add-image https://developers.google.com/slides/how-tos/add-image

更新日期:2020 年 2 月 7 日

从 2020 年 1 月开始,访问令牌不能与查询参数一起使用,例如access_token=###. Ref https://cloud.google.com/blog/products/application-development/upcoming-changes-to-the-google-drive-api-and-google-picker-api因此,请使用请求标头的访问令牌而不是查询参数。

就OP的情况而言,我认为这个答案 https://stackoverflow.com/a/60085036/7108653可以使用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 DriveApp.getFileById 从 Google Drive 到 Google Slides 的图像 的相关文章

随机推荐