使用 Appscript 将 excel 文件转换为 google 表格时不断抛出 Drive API 错误“找不到文件”

问题描述 投票:0回答:1

我正在开发一个应用程序脚本,它将 Excel 附件从我的收件箱下载到文件夹 A,然后将其转换为 google 工作表并保存在同一文件夹 A 中。我已经使用了之前利用 Drive API 的 thread 的指导服务,但在最后一行继续收到以下错误“对drive.files.insert的API调用失败并出现错误:文件未找到:WIP”

我已启用“supportsAllDrives”并尝试保存在不同的文件夹中,但没有成功。请查看下面的代码

function save_new_WIP() {

  //Looks for email attachment
  var threads = GmailApp.search('in:inbox subject:"WIP Update" to: "xxxxxxxxx"');
  var message = threads[0].getMessages();
  var attachment = message[message.length - 1].getAttachments()[0];

 

  //saves excel file in drive
  var folder = DriveApp.getFolderById("1uFvXhIIst_ksjZysyH-09TYeo5k44YvO");
  var fileName = attachment.getName();
  var newWIP = folder.createFile(attachment).setName(fileName);
  
  var ID = newWIP.getId();
  
 
  //converts xlxs excel file to google sheet.

  const excel = DriveApp.getFileById(ID);
  let file = {
    title: 'converted',
    mimeType: MimeType.GOOGLE_SHEETS,
    parents: [{id: folder}],
    supportsAllDrives: true 
  };

  file = Drive.Files.insert(file, excel);
  
}
google-apps-script google-sheets google-drive-api
1个回答
0
投票

修改要点:

  • 当我看到你的剧本时,似乎
    folder
    parents: [{id: folder}],
    var folder = DriveApp.getFolderById("1uFvXhIIst_ksjZysyH-09TYeo5k44YvO");
    。在本例中,
    folder
    是类文件夹对象。我认为这可能是您当前问题的原因。

当这些点都体现在你的脚本中时,下面的修改如何?

来自:

let file = {
  title: 'converted',
  mimeType: MimeType.GOOGLE_SHEETS,
  parents: [{id: folder}],
  supportsAllDrives: true 
};

致:

let file = {
  title: 'converted',
  mimeType: MimeType.GOOGLE_SHEETS,
  parents: [{ id: folder.getId() }], // Modified
  supportsAllDrives: true 
};

注:

当你的整个脚本修改后,它就变成如下。

function save_new_WIP() {
  var threads = GmailApp.search('in:inbox subject:"WIP Update" to: "xxxxxxxxx"');
  var message = threads[0].getMessages();
  var attachment = message[message.length - 1].getAttachments()[0];
  var folder = DriveApp.getFolderById("1uFvXhIIst_ksjZysyH-09TYeo5k44YvO");
  var fileName = attachment.getName();
  var newWIP = folder.createFile(attachment).setName(fileName);
  var ID = newWIP.getId();
  const excel = DriveApp.getFileById(ID);
  let file = {
    title: 'converted',
    mimeType: MimeType.GOOGLE_SHEETS,
    parents: [{ id: folder.getId() }],
    supportsAllDrives: true
  };
  file = Drive.Files.insert(file, excel);
}

参考:

© www.soinside.com 2019 - 2024. All rights reserved.