该脚本试图发送一封电子邮件,其中附加了一个转换为 PDF 文件的工作表 zip 文件。只有带有内容的工作表才能包含在 zip 文件中。
function zipit() {
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/[Google Key]/edit#");
// Base URL
var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
var print1 = ss.getSheetByName("Print1");
var print2A = ss.getSheetByName("Print2A");
var print2B = ss.getSheetByName("Print2B");
var print2C = ss.getSheetByName("Print2C");
var print3A = ss.getSheetByName("Print3A");
//get email address to send to
var organiserEmail = calcSheet.getRange("E31").getValue();
//get date range for next month
var tablesSheet = ss.getSheetByName("Next Month");
var period = print1.getRange("V2:W2").getValue(); // period for the league taken from League 1
var subject = "Singles League print outputs for : " + period;
var body = "Attached are the PDF files to allow you to print the tables";
var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
+ '&size=A4' // paper size legal / letter / A4
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true' // fit to page width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
//+ '&printRange'
+ '&pagenumbers=false' // hide page numbers
+ '&gridlines=true' // hide gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&gid='; // the sheet's Id
// Use token to access the sheet
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
'muteHttpExceptions': true,
}
};
// make empty array to hold fetched blobs
var blobs = [];
var i=0; // Set start number of 'blobs[]'
// **league 1**
var SheetId = '56'; // id of League 1 print sheet - 'print1'
var filename = '1' + '.pdf';
// create PDF
// Convert individual worksheets to PDF
try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
catch(err) { Browser.msgBox("Error"); }
// Convert the response to a blob and store in array
blobs[i] = response.getBlob().setName(filename);
i=i+1; // Set number of 'blob'
// **League 2A**
// Check if sheet has content
if (print2A.getRange("A11").getValue() != null) {
SheetId = '58'; // id of League 1 print sheet - 'print2A'
// create PDF
// Convert individual worksheets to PDF
try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
catch(err) { Browser.msgBox("Error"); }
// Convert the response to a blob and store in array
blobs[i] = response.getBlob().setName('2A' + '.pdf');
i=i+1; // Set number of 'blob'
}
// **League 2B**
// Check if sheet has content
if (print2B.getRange("A11").getValue() != null) {
SheetId = '59'; // id of League 2B print sheet - 'print2B'
// create PDF
// Convert individual worksheets to PDF
try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
catch(err) { Browser.msgBox("Error"); }
// Convert the response to a blob and store in array
blobs[i] = response.getBlob().setName('2B' + '.pdf');
i=i+1; // Set number of 'blob'
}
// **League 2C**
//Check if sheet has content
if (print2C.getRange("A11").getValue() != null) {
SheetId = '62'; // id of League 2C print sheet - 'print2C'
// create PDF
// Convert individual worksheets to PDF
try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
catch(err) { Browser.msgBox("Error"); }
// Convert the response to a blob and store in array
blobs[i] = response.getBlob().setName('2C' + '.pdf');
var i=i+1; // Set number of 'blob'
}
// **League 3A**
// Check if sheet has content
if (print3A.getRange("A11").getValue() != null) {
SheetId = '60'; // id of League 3A print sheet - 'print3A'
// create PDF
// Convert individual worksheets to PDF
try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
catch(err) { Browser.msgBox("Error"); }
// Convert the response to a blob and store in array
blobs[i] = response.getBlob().setName('3A' + '.pdf');
i=i+1; // Set number of 'blob'
}
/************************************************/
// New Participants
SheetId = '38'; // id of New Participants print sheet - 'New Participants Alpha'
// create PDF
// Convert individual worksheets to PDF
try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
catch(err) { Browser.msgBox("Error"); }
// Convert the response to a blob and store in array
blobs[i] = response.getBlob().setName('Participants' + '.pdf');
/************************************************/
// Send email
// create a new blob that is a zip file containing our blob array
var zipBlob = Utilities.zip(blobs).setName('Singles League :' + period + '.zip');
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(organiserEmail, subject, body, {
htmlBody: body,
attachments:[zipBlob]
});
}
想法是打印第一张纸(联赛 1),然后转到联赛 2A,检查单元格 A11(1,11) 以查看是否有任何内容......如果该单元格中有内容然后转换页面转换为 PDF 并创建一个“blob”...如果 A11 中没有内容,则移动到 League 2B,测试单元格 A11 的内容,如果没有内容,则移动到 League 2C,如果有内容,则将页面转换为 PDF 并创建一个'blob'... 以此类推,直到最后一页称为 Participants。最后的任务是创建一个 zipfile 并发送带有 zipfile 附件的电子邮件。
问题是单元格“A11”的测试不起作用。所有工作表(1.pdf、2A.pdf、2B.pdf、2C.pdf、3A.pdf 和 Participants.pdf)都在 zip 文件中,无论它们是否没有内容。例程的电子邮件部分正常工作。参与者表没有打印,而是有 3A 的副本......有人可以告知发生了什么并建议更正吗?感谢期待。
在工作代码下方记录以实现目标....下面仅包含最后一部分:-
// Get token to access the sheet
var token = ScriptApp.getOAuthToken();
var options = {
headers:{
'Authorization': 'Bearer ' + token,
'muteHttpExceptions': true,
}
};
var SheetId = '56'; // id of League 1 print sheet - 'print1'
var filename = '1' + '.pdf';
// create PDF
// Convert individual worksheets to PDF
try {
var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
}
catch(err)
{
Browser.msgBox("Error");
}
// Convert the response to a blob and store in array
blobs.push(response.getBlob().setName(filename));
// League 2A
// Check if sheet has content
if ((print2A.getRange("A11").getValue())!= ""){
SheetId = '58'; // id of League 1 print sheet - 'print2A'
filename = '2A' + '.pdf';
// create PDF
// Convert individual worksheets to PDF
try {
var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
}
catch(err)
{
Browser.msgBox("Error");
}
// Convert the response to a blob and store in array
blobs.push(response.getBlob().setName(filename));
}
// League 2B
// Check if sheet has content
if ((print2B.getRange("A11").getValue())!= ""){
SheetId = '59'; // id of League 2B print sheet - 'print2B'
filename = '2B' + '.pdf';
// create PDF
// Convert individual worksheets to PDF
try {
var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
}
catch(err)
{
Browser.msgBox("Error");
}
// Convert the response to a blob and store in array
blobs.push(response.getBlob().setName(filename));
}
// League 2C
//Check if sheet has content
if ((print2C.getRange("A11").getValue())!= ""){
SheetId = '62'; // id of League 2C print sheet - 'print2C'
filename = '2C' + '.pdf';
// create PDF
// Convert individual worksheets to PDF
try {
var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
}
catch(err)
{
Browser.msgBox("Error");
}
// Convert the response to a blob and store in array
blobs.push(response.getBlob().setName(filename));
}
// League 3A
// Check if sheet has content
if ((print3A.getRange("A11").getValue())!= ""){
SheetId = '60'; // id of League 3A print sheet - 'print3A'
filename = '3A' + '.pdf';
// create PDF
// Convert individual worksheets to PDF
try {
var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
}
catch(err)
{
Browser.msgBox("Error");
}
// Convert the response to a blob and store in array
blobs.push(response.getBlob().setName(filename));
}
// League 3B
// Check if sheet has content
if ((print3A.getRange("A11").getValue())!= ""){
SheetId = '61'; // id of League 3B print sheet - 'print3B'
filename = '3B' + '.pdf';
// create PDF
// Convert individual worksheets to PDF
try {
var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
}
catch(err)
{
Browser.msgBox("Error");
}
// Convert the response to a blob and store in array
blobs.push(response.getBlob().setName(filename));
}
/************************************************/
// New Participants
SheetId = '38'; // id of New Participants print sheet - 'New Participants Alpha'
filename = 'Participants' + '.pdf';
// create PDF
// Convert individual worksheets to PDF
try {
var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
}
catch(err)
{
Browser.msgBox("Error");
}
// Convert the response to a blob and store in array
blobs.push(response.getBlob().setName(filename));
/************************************************/
// Send email
// create a new blob that is a zip file containing our blob array
var zipBlob = Utilities.zip(blobs).setName('Singles League :' + period + '.zip');
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0) {
GmailApp.sendEmail(organiserEmail, subject, body, {
htmlBody: body,
attachments:[zipBlob]
});
};
}