Google App Script通过电子邮件发送整个工作簿,而不是单个工作表

时间:2018-06-27 09:25:04

标签: javascript google-apps-script google-sheets

我将这些 sort 作品拼凑而成的伪记录。

本质上,我想做的是通过电子邮件发送一个标签,该标签每天将公式保存在公司内部。

目前,这是可行的,但它是通过电子邮件发送整张纸而不是一张纸。我在底部附近嵌入了一个函数以隐藏工作表,但是我很确定它没有被执行。我刚开始使用Python编码,所以JS对我来说一直很困难。为此表示歉意。

此外,有人会碰巧知道如何将密码嵌入PDF吗? (这是很不错的,没有必要)。

       // Simple function to send Weekly Status Sheets to contacts listed on the "Contacts" sheet in the MPD.

// Load a menu item called "Project Admin" with a submenu item called "Send Status"
// Running this, sends the currently open sheet, as a PDF attachment
function onOpen() {
  var submenu = [{name:"Send Status", functionName:"exportSomeSheets"}];
  SpreadsheetApp.getActiveSpreadsheet().addMenu('Project Admin', submenu);  
}

function exportSomeSheets() {
  // Set the Active Spreadsheet so we don't forget
  var originalSpreadsheet = SpreadsheetApp.getActive();

  // Set the message to attach to the email.
  var message = "Daily Sales Snapshot"; // Could make it a pop-up perhaps, but out of wine today

  // Get Project Name from Cell A1
  var projectname = originalSpreadsheet.getRange("H1:H1").getValues(); 
  // Get Reporting Period from Cell B3
  var period = originalSpreadsheet.getRange("B3:B3").getValues(); 
  // Construct the Subject Line
  var subject = projectname + " - Daily Status Sheet - " + period;


  // Get contact details from "Contacts" sheet and construct To: Header
  // Would be nice to include "Name" as well, to make contacts look prettier, one day.
  var contacts = originalSpreadsheet.getSheetByName("Contacts");
  var numRows = contacts.getLastRow();
  var emailTo = contacts.getRange(2, 2, numRows, 1).getValues();

  // Google scripts can't export just one Sheet from a Spreadsheet
  // So we have this disgusting hack

var newSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = newSpreadsheet.getSheets;
  for (var i = 5; i < sheets.length; i++) {
    if (sheets[i].getSheetName() != sheetName) {
      sheet[i].hideSheet();
    }
  }

  // Make zee PDF, currently called "Weekly status.pdf"
  // When I'm smart, filename will include a date and project name
  var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
  var attach = {fileName:'Daily Status.pdf',content:pdf, mimeType:'application/pdf'};

  // Send the freshly constructed email 
  MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});


}

1 个答案:

答案 0 :(得分:0)

不能仅导出单个工作表,而是快速搜索shows this gist,它具有“简单的Google Apps脚本将单个工作表导出为PDF并将其通过电子邮件发送到联系人列表”的功能。您可以根据需要进行调整。

基本上,该脚本将创建一个新的电子表格,将所需的单个工作表复制到该新的电子工作表中,删除自动生成的“工作表1”,发送一封包含新电子表格为pdf的电子邮件,然后删除新的电子工作表。

在此处粘贴该代码以供参考(以防被删除):

function exportSomeSheets() {
  // Set the Active Spreadsheet so we don't forget
  var originalSpreadsheet = SpreadsheetApp.getActive();

  // Set the message to attach to the email.
  var message = "Please see attached"; // Could make it a pop-up perhaps, but out of wine today

  // Get Project Name from Cell A1
  var projectname = originalSpreadsheet.getRange("A1:A1").getValues(); 
  // Get Reporting Period from Cell B3
  var period = originalSpreadsheet.getRange("B3:B3").getValues(); 
  // Construct the Subject Line
  var subject = projectname + " - Weekly Status Sheet - " + period;


  // Get contact details from "Contacts" sheet and construct To: Header
  // Would be nice to include "Name" as well, to make contacts look prettier, one day.
  var contacts = originalSpreadsheet.getSheetByName("Contacts");
  var numRows = contacts.getLastRow();
  var emailTo = contacts.getRange(2, 2, numRows, 1).getValues();

  // Google scripts can't export just one Sheet from a Spreadsheet
  // So we have this disgusting hack

  // Create a new Spreadsheet and copy the current sheet into it.
  var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var projectname = SpreadsheetApp.getActiveSpreadsheet();
  sheet = originalSpreadsheet.getActiveSheet();
  sheet.copyTo(newSpreadsheet);

  // Find and delete the default "Sheet 1", after the copy to avoid triggering an apocalypse
  newSpreadsheet.getSheetByName('Sheet1').activate();
  newSpreadsheet.deleteActiveSheet();

  // Make zee PDF, currently called "Weekly status.pdf"
  // When I'm smart, filename will include a date and project name
  var pdf = DocsList.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
  var attach = {fileName:'Weekly Status.pdf',content:pdf, mimeType:'application/pdf'};

  // Send the freshly constructed email 
  MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});

  // Delete the wasted sheet we created, so our Drive stays tidy.
  DocsList.getFileById(newSpreadsheet.getId()).setTrashed(true);  
}

不知道creator of the script,但喜欢他的风格。