【问题标题】:Google app scripts: email a spreadsheet as excel谷歌应用脚​​本:将电子表格作为 excel 发送电子邮件
【发布时间】:2015-10-26 21:35:42
【问题描述】:

如何制作一个应用脚本,将电子表格作为 excel 文件附加并通过电子邮件发送到某个电子邮件地址?

Stackoverflow 上有一些关于如何执行此操作的较早帖子,但是它们现在似乎已经过时,并且 似乎不起作用

谢谢。

【问题讨论】:

  • 向我们展示您尝试过什么但没有奏效。
  • 你有没有试过实现一些代码?您可以参考应用程序脚本文档来实现这一点。为此,您应该获得具有 mime 类型 excel 的电子表格文件的导出链接,并作为附件发送到电子邮件。您是否检查过 Markus Uhl 在post 中的最新答案?希望有帮助!
  • 你好 Zig 和 KRR。我已经在上面最近的帖子中尝试了这个建议(由 Markus 撰写),但有几个问题。主要是使用 var driveService = getDriveService();它说它是“找不到”。所以我一定是在哪里错过了一步??
  • @Bhavin,您是否按照 Markus 引用的 github 链接中提到的 setup 步骤进行操作?请添加有关您尝试过的内容的更多详细信息。谢谢!

标签: google-apps-script google-sheets google-drive-api export-to-excel


【解决方案1】:

这是一个最新的工作版本。此 Google Apps 脚本运行的一个先决条件是必须启用 Drive API v2 Advanced Google Service。通过 Resources -> Advanced Google Services... -> Drive API v2 -> on 在您的 Google Apps 脚本中启用它。然后,该窗口会告诉您必须在 Google Developers Console 中启用此服务。点击链接并在那里启用服务!完成后,只需使用此脚本即可。

/**
 * Thanks to a few answers that helped me build this script
 * Explaining the Advanced Drive Service must be enabled: http://stackoverflow.com/a/27281729/1385429
 * Explaining how to convert to a blob: http://ctrlq.org/code/20009-convert-google-documents
 * Explaining how to convert to zip and to send the email: http://ctrlq.org/code/19869-email-google-spreadsheets-pdf
 * New way to set the url to download from by @tera
 */
function emailAsExcel(config) {
  if (!config || !config.to || !config.subject || !config.body) {
    throw new Error('Configure "to", "subject" and "body" in an object as the first parameter');
  }

  var spreadsheet   = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = spreadsheet.getId()
  var file          = Drive.Files.get(spreadsheetId);
  var url           = 'https://docs.google.com/spreadsheets/d/'+spreadsheetId+'/export?format=xlsx';
  var token         = ScriptApp.getOAuthToken();
  var response      = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });

  var fileName = (config.fileName || spreadsheet.getName()) + '.xlsx';
  var blobs   = [response.getBlob().setName(fileName)];
  if (config.zip) {
    blobs = [Utilities.zip(blobs).setName(fileName + '.zip')];
  }

  GmailApp.sendEmail(
    config.to,
    config.subject,
    config.body,
    {
      attachments: blobs
    }
  );
}

更新:我更新了设置下载网址的方式。通过 file.exportLinks 集合执行此操作不再有效。感谢@tera 在他的回答中指出这一点。

【讨论】:

    【解决方案2】:

    看起来@Christiaan Westerbeek 的回答是正确的,但距离他的帖子已经过去一年了,我认为需要对他上面给出的脚本进行一些修改。

    var url = file.exportLinks[MimeType.MICROSOFT_EXCEL];
    

    这行代码有问题,可能exportLinks 现在已经贬值了。当我执行他的代码时,它给出了以下效果的错误:

    TypeError:无法从未定义中读取属性“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”。

    解决方法如下:

    上述代码行中的 URL 基本上是“下载为 xlsx”的 URL,可用于直接将电子表格下载为您从 File> Download as > Microsoft Excel (.xlsx) 获得的 xlsx 文件

    这是格式:

    https://docs.google.com/spreadsheets/d/<<<ID>>>/export?format=xlsx&id=<<<ID>>> 其中 > 应替换为文件的 ID。

    查看here 以轻松了解如何从您的 Google 表格的 URL 中提取 ID。

    【讨论】:

    • 我觉得https://docs.google.com/spreadsheets/d/<<<ID>>>/export?format=xlsx 就够了。
    • 同意.. 确认
    猜你喜欢
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 2020-04-22
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多