【问题标题】:Image from Google Drive to Google Slides via DriveApp.getFileById通过 DriveApp.getFileById 从 Google Drive 到 Google Slides 的图像
【发布时间】:2017-09-04 01:25:17
【问题描述】:

我想在 Google Developers - How to... 指令之后通过 Google Apps 脚本将图像导入 Google 幻灯片

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- and Slides API 已在 Google 高级服务中激活。文件夹和文件具有公共共享。

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

【问题讨论】:

  • 关于你的错误信息The provided image is in an unsupported format.,请问你要使用的图片格式是什么?
  • 我尝试了 GIF-、JPG-、PNG- 和 Google 绘图文件中的 ID。
  • 别担心。 Google 可以使用这些格式。
  • 同样的错误信息,当我使用像“var imageUrl = 'drive.google.com/uc?id=0B6cQGlkNU4tMU.....';”这样的直接链接时只有这个 imageUrl 有效:“lh6.googleusercontent.com/vuINetSxcBd8InRV.....=w1919-h986”。但我想在指令中使用 DriveApp.getFileById。
  • 我能问一下你目前的剧本吗?如果可以,请将其添加到您的问题中。

标签: google-apps-script google-drive-api google-slides-api


【解决方案1】:

下面的修改怎么样?

发件人:

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

收件人:

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

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

更新日期:2020 年 2 月 7 日

自 2020 年 1 月起,访问令牌不能与 access_token=### 等查询参数一起使用。 Ref所以请使用请求头的访问令牌而不是查询参数。

在OP的情况下,我认为可以使用this answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多