Google Apps脚本通过电子邮件发送活动电子表格 [英] Google Apps Script to Email Active Spreadsheet

查看:148
本文介绍了Google Apps脚本通过电子邮件发送活动电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线发现了一个脚本,它将当前表单复制到临时的新电子表格中,并将其转换为PDF并通过电子邮件发送给它。我能够得到它的工作,但试图设置它,以便它只发送一定范围。试图玩一下,但我不是一个很好的编码器。另外,我也会想知道如何使它适合横向1页的PDF格式,无需网格线转换(我的在线研究显示这不可能?),甚至可以作为XLS发送。

I found a script online that takes the current sheet, copies it to a temporary new spreadsheet, converts it to PDF and emails it. I was able to get it working but trying to set it up so that it only sends a certain range. Tried to play with it a bit but I am not a good coder by any stretch. Alternatively I'd be interested also in figuring out how to get it to fit to 1 page PDF in landscape, convert without gridlines (my online research shows this ins't possible?) or even send as XLS.

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


推荐答案

自动发送带有pdf格式电子邮件的Google电子表格。它工作正常,但我需要通过删除网格线并设置显示A4来自定义pdf。有没有办法?谢谢

I am using this code to automatically send a google spreadsheet with email in pdf format. it works fine, but I need to customize the pdf by removing the grid lines and set display A4 . Is there a way ? thanks

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:"Invia", functionName:"exportSomeSheets"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu('Invia PDF', 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 = "Messaggio email";
// Get Project Name from Cell A1
var projectname = originalSpreadsheet.getRange("A1:A1").getValues(); 
// Get Reporting Period from Cell A2
var period = originalSpreadsheet.getRange("A2:A2").getValues(); 
// Construct the Subject Line
var subject = projectname;


// 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 emailTo = originalSpreadsheet.getRange("A3:A3").getValues();


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

var aliases = GmailApp.getAliases()
Logger.log(aliases);
Logger.log(aliases[2]); //returns the alias located at position 0 of the aliases array


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

// Send the freshly constructed email 
GmailApp.sendEmail(emailTo, subject, message, {'from': aliases[2], attachments:[attach]});

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

这篇关于Google Apps脚本通过电子邮件发送活动电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆