【问题标题】:How to use Drive v3 in google app script?如何在谷歌应用脚​​本中使用 Drive v3?
【发布时间】:2021-05-14 12:56:19
【问题描述】:

我有以下代码,我想在 App Script 中使用 Drive v3

function myFunction() {
  let id = "<YOUR ODS FILE ID>";
  let file = DriveApp.getFileById(id);

  let fileBlob = file.getBlob();


  newFile = {
    name: "New File",
    mimeType: "application/vnd.google-apps.spreadsheet"
  }

  try{
    Drive.Files.create(newFile, fileBlob);
  }catch(e){
    Logger.log("Error");
    Logger.log(e);
  }
}

但是,默认情况下,谷歌应用脚​​本只提供 v2。文档似乎并不容易直接在 App Script 中进行此迁移。

如何在此代码中直接在 google 应用脚本中使用 Drive v3?

【问题讨论】:

  • Google Drive 的 GAS built-inadvanced service 都利用了 API 的 V2。要使用 V3,您需要直接使用 Url Fetch 向 REST API 发出请求。
  • @TheAddonDepot 你能帮我如何使用 Drive v3 和 url fetch 吗?

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


【解决方案1】:

我相信你的目标如下。

  • 您想将let file = DriveApp.getFileById(id) 的文件(let id = "&lt;YOUR ODS FILE ID&gt;" 的 ODS 文件)转换为 Google 电子表格。
  • 您希望使用 Drive API v3 和 Google Apps 脚本的 UrlFetchApp 来实现此目的。

修改点:

  • 虽然很遗憾,我不确定您的问题中let file = DriveApp.getFileById(id) 的文件大小,但我认为在您的情况下,文件内容需要以multipart/form-data 发送。 Ref
  • 在高级 Google 服务中,multipart/form-data 是在内部服务器端实现的。但是,当您想使用 UrlFetchApp 实现此目的时,需要创建请求正文。

当以上几点反映到示例脚本中时,它变成如下。

示例脚本:

在这种情况下,使用 Drive API。所以please enable Drive API at Advanced Google services

function myFunction() {
  const fileId = "<YOUR ODS FILE ID>"; // Please set the file ID.

  const metadata = {
    name: "New File",
    mimeType: MimeType.GOOGLE_SHEETS,
    // parents: ["### folder ID ###"], // If you want to put the converted Spreadsheet to the specific folder, please use this.
  };
  const payload = {
    metadata: Utilities.newBlob(JSON.stringify(metadata), "application/json"),
    file: DriveApp.getFileById(fileId).getBlob(),
  };
  const options = {
    method: "post",
    payload: payload,
    headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() },
  };
  const url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
  const res = UrlFetchApp.fetch(url, options).getContentText();
  console.log(res);

  // DriveApp.createFile(blob)  // This is used for automatically detecting the scope of "https://www.googleapis.com/auth/drive".
}

注意:

  • uploadType=multipart方法,官方文档说如下。

    使用此上传类型可在单个请求中快速传输小文件(5 MB 或更少)和描述文件的元数据。

    • 当您想为此使用超过 5 MB 的文件时,请使用可恢复上传。 Ref
  • 顺便说一句,在您的脚本中,似乎使用了您 Google Drive 上的 ODF 文件。在这种情况下,您也可以使用 Drive API v3 中的“文件:复制”的方法来实现您的目标。示例脚本如下。

      function myFunction2() {
        const fileId = "<YOUR ODS FILE ID>"; // Please set the file ID.
    
        const url = `https://www.googleapis.com/drive/v3/files/${fileId}/copy`;
        const params = {
          method: "post",
          headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`},
          contentType: "application/json",
          payload: JSON.stringify({name: "New name", mimeType: MimeType.GOOGLE_SHEETS})
        };
        const res = UrlFetchApp.fetch(url, params);
        console.log(res.getContentText())
      }
    

参考资料:

【讨论】:

  • 相信你明白我想做什么。但是当我使用您的示例时,我收到以下错误消息`异常:请求失败googleapis.com返回代码500。截断的服务器响应:{“错误”:{“错误”:[{“域”:“全局”,“原因”:“内部错误”,“消息”:“内部错误”}],“代码”:500,`
  • @Diego Resende 感谢您的回复。我带来的不便表示歉意。我在回答中提出了 2 个示例脚本。但是,从您对when I use your example I get the following error message 的回复来看,我无法理解您使用的是哪个脚本。我为我糟糕的英语水平道歉。我可以问你关于你当前剧本的细节吗?顺便说一句,当我测试我的 2 个示例脚本时,没有发生错误。 ODS 文件可以转换为 Google 电子表格。对于这种情况,我深表歉意。
  • 我使用了这两个脚本,但总是得到相同的答案。 ODS 文件有 56 列、16,876 行的数据。该文件的大小为 2mb。我只想将 ODS 文件转换为 google 表格
  • @Diego Resende 感谢您的回复。我带来的不便表示歉意。不幸的是,我无法复制您的情况。那么,为了确认您的情况,您能否提供您的示例文件来复制您的问题?借此,我想确认一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多