Google apps script 谷歌应用程序脚本:以excel格式发送电子表格

Google apps script 谷歌应用程序脚本:以excel格式发送电子表格,google-apps-script,google-sheets,google-drive-api,export-to-excel,Google Apps Script,Google Sheets,Google Drive Api,Export To Excel,如何制作一个应用程序脚本,将电子表格作为excel文件附加到某个电子邮件地址 Stackoverflow上有一些关于如何做到这一点的老帖子,但它们现在似乎已经过时,似乎不起作用 多谢各位 这是一个最新的工作版本。此Google Apps脚本工作的一个先决条件是必须启用驱动器API v2。通过资源->高级谷歌服务在谷歌应用程序脚本中启用它…->驱动API v2->on。然后,该窗口将告诉您还必须在Google开发者控制台中启用此服务。按照链接并在那里启用服务!完成后,只需使用此脚本 /** *

如何制作一个应用程序脚本,将电子表格作为excel文件附加到某个电子邮件地址

Stackoverflow上有一些关于如何做到这一点的老帖子,但它们现在似乎已经过时,似乎不起作用


多谢各位

这是一个最新的工作版本。此Google Apps脚本工作的一个先决条件是必须启用驱动器API v2。通过资源->高级谷歌服务在谷歌应用程序脚本中启用它…->驱动API v2->on。然后,该窗口将告诉您还必须在Google开发者控制台中启用此服务。按照链接并在那里启用服务!完成后,只需使用此脚本

/**
 * 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
    }
  );
}

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

看起来@Christiaan Westerbeek的回答是正确的,但他发表这篇文章已经一年了,我认为需要对他上面给出的脚本进行一些修改

var url = file.exportLinks[MimeType.MICROSOFT_EXCEL];
这行代码有问题,可能是
exportLinks
现在已经贬值了。当我执行他的代码时,它给出了一个错误,大意如下:

TypeError:无法从未定义中读取属性“
应用程序/vnd.openxmlformats of icedocument.spreadsheetml.sheet

解决办法如下:

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

格式如下:

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


检查以轻松理解如何从google工作表的URL中提取ID。

向我们展示您尝试过的和没有成功的地方。您是否尝试过实现一些代码?您可以参考应用程序脚本文档来实现这一点。为此,您应该使用mime类型的excel获取电子表格文件的导出链接,并将其作为附件发送到电子邮件。你查过Markus Uhl最近的答案了吗?希望有帮助!Zig和KRR你好。我在上面(Markus)最近的帖子中尝试过这个建议,但有几个问题。主要是使用var driveService=getDriveService();上面说的是“找不到”。所以我肯定错过了一个步骤,在那里???@Bhavin,你是否遵循了Markus提到的github链接中提到的步骤?请添加有关您尝试过的内容的更多详细信息。谢谢我想
https://docs.google.com/spreadsheets/d//export?format=xlsx
足够了。同意。。证实