【问题标题】:Google app script to fetch xlsx file from email and copy file to Google sheet谷歌应用脚​​本从电子邮件中获取 xlsx 文件并将文件复制到谷歌表
【发布时间】:2021-02-23 18:42:59
【问题描述】:

我是使用应用脚本的初学者,目前我只是在复制示例。我希望有人可以帮助我编写下面的脚本。它旨在从我的 Gmail 中获取一个 excel 文件并将其复制到 Google 工作表中。我从这里的一个问题中看到了这个脚本,但不能让它工作。谷歌表格文件在这里:https://docs.google.com/spreadsheets/d/1rED5zphZKp7JIK9qaTpHLKBSMsSiLAvhBAXc6Tn0zqI/edit#gid=301517543

脚本:

function getRawData() {
  var threads = GmailApp.search('subject:myRentokil Report Delivery: Canpac | Pest Activity');
  var message = threads[0].getMessages()[0];
  var attachment = message.getAttachments()[0];

  var xlsxBlob = attachment[0]; // Is supposes that attachment[0] is the blob of xlsx file.
  var convertedSpreadsheetId = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS}, xlsxBlob).id;
  var sheet = SpreadsheetApp.openById(convertedSpreadsheetId).getSheets()[0]; // There is the data in 1st tab.
  var data = sheet.getDataRange().getValues();
  Drive.Files.remove(convertedSpreadsheetId); // Remove the converted file.

  var sheet = SpreadsheetApp.openById("1rED5zphZKp7JIK9qaTpHLKBSMsSiLAvhBAXc6Tn0zqI").getSheetByName("RawData");
  sheet.clearContents();
  var range = sheet.getRange(1, 1, data.length, data[0].length);range.setValues(data);

 }

【问题讨论】:

  • 尝试在脚本编辑器的服务部分开启DriveAPI。
  • 错误信息是什么?
  • 嗨,它现在可以工作了,但是,我必须删除“Drive.Files.remove(convertedSpreadsheetId);”行这意味着转换后的纸张会堆积在我的驱动器上。如何自动删除临时工作表。错误信息显示:Error Document 15ntCXkDasViXnrqlTfI6hjWvAch_hXqS8a2r9ybmXRU is missing(可能已被删除,或者您没有读取权限?)

标签: google-apps-script


【解决方案1】:

修改后的脚本

function getRawData() {
    // This gets the threads
    var threads = GmailApp.search('subject:excel attachment');
    var message = threads[0].getMessages()[0]; // returns the first message from the first thread
    var attachment = message.getAttachments()[0]; // returns the first attachment
    
    var xlsxBlob = attachment[0]; // This is a blob, yes.
    
    var convertedSpreadsheetId = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS}, xlsxBlob).id;
    var sheet = SpreadsheetApp.openById(convertedSpreadsheetId).getSheets()[0];
    var data = sheet.getDataRange().getValues(); // gets values from first tab
    
    // You were overwriting some variables here, so changed to dataSheet and dataRange
    var dataSheet = SpreadsheetApp.openById("1rED5zphZKp7JIK9qaTpHLKBSMsSiLAvhBAXc6Tn0zqI").getSheetByName("RawData");
    var dataRange = dataSheet.getRange(1, 1, data.length, data[0].length);
    dataRange.setValues(data);
    
    // Moved this line to the end because you were deleting it before using the data inside it.
    // Changed to trash instead of remove.
    Drive.Files.trash(convertedSpreadsheetId); // Remove the converted file.
 }
  • 我评论了你的脚本以使步骤更清晰。

  • 有一些变量被初始化了两次,所以只是更改了这些名称。

  • 错误的原因是 remove 在 Drive API v2 中不作为方法存在。

    https://developers.google.com/drive/api/v2/reference/files/trash

    remove 实际上似乎确实有效并删除了文件,但它总是返回错误。 trash 按预期工作。如果你想跳过垃圾箱,你可以delete 代替。

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 2013-04-04
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    相关资源
    最近更新 更多