Javascript 试图通过电子邮件发送谷歌工作表,但未定义响应
我有10张左右的表格,上面写着每个摊主白天的销售额,每一张都会在晚上通过电子邮件发送给摊主。所有人都有相同的脚本发送电子表格,但现在有3个出现错误“响应未定义” 请找个人来帮我 请看下面我的脚本Javascript 试图通过电子邮件发送谷歌工作表,但未定义响应,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我有10张左右的表格,上面写着每个摊主白天的销售额,每一张都会在晚上通过电子邮件发送给摊主。所有人都有相同的脚本发送电子表格,但现在有3个出现错误“响应未定义” 请找个人来帮我 请看下面我的脚本 /* Send Spreadsheet in an email as PDF, automatically */ function emailSpreadsheetAsPDF() { // Send the PDF of the spreadsheet to this email address
/* Send Spreadsheet in an email as PDF, automatically */
function emailSpreadsheetAsPDF() {
// Send the PDF of the spreadsheet to this email address
const email = "Email for the stallholder";
// Get the currently active spreadsheet URL (link)
// Or use SpreadsheetApp.openByUrl("<>");
const ss = SpreadsheetApp.getActiveSpreadsheet();
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
today = dd+'/'+mm+'/'+yyyy;
console.log(today);
// Subject of email message
const subject = ("Daily Sales Summary - " + today);
// Email Body can be HTML too with your logo image - see ctrlq.org/html-mail
const body = "<br>Hi Julie,<br>" + "<br>Please find attached your Daily Sales Summary.<br>" + "<br>Kind Regards,<br>" + "<br>"
console.log('\n')
// Base URL
const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId());
const exportOptions =
'exportFormat=pdf&format=pdf'
'&size=A4'
'&portrait=true' + // orientation, false for landscape
'&fitw=true&source=labnol' + // fit to page width, false for actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid='; // the sheet's Id
const token = ScriptApp.getOAuthToken();
const sheets = ss.getSheets();
// make an empty array to hold your fetched blobs
const blobs = [];
for (var i = 0; i < sheets.length; i += 1) {
// Convert individual worksheets to PDF
const response = UrlFetchApp.fetch(url + exportOptions + sheets[i].getSheetId(), {
headers: {
Authorization: 'Bearer ' + token
}
})
// convert the response to a blob and store in our array
blobs[i] = response.getBlob().setName('Daily Sales');
}
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [response]
});
}
/*自动以PDF格式通过电子邮件发送电子表格*/
函数emailSpreadsheetAsPDF(){
//将电子表格的PDF发送到此电子邮件地址
const email=“摊贩电子邮件”;
//获取当前活动的电子表格URL(链接)
//或者使用SpreadsheetApp.openByUrl(“”);
const ss=SpreadsheetApp.getActiveSpreadsheet();
var today=新日期();
var dd=today.getDate();
var mm=today.getMonth()+1;
var yyyy=today.getFullYear();
如果(dd这个答案怎么样?请把它看作是几个可能的答案之一
修改点:
- 在脚本的以下部分,
exportOptions
只返回exportFormat=pdf&format=pdf
。因为+
操作符不是在'exportFormat=pdf&format=pdf'
之后添加的
const exportOptions =
'exportFormat=pdf&format=pdf'
'&size=A4'
'&portrait=true' + // orientation, false for landscape
'&fitw=true&source=labnol' + // fit to page width, false for actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid='; // the sheet's Id
const response=UrlFetchApp.fetch(######)
被放入for循环中。并且attachments:[response]
被用于for循环外。由此,出现未定义响应“
的错误
- 顺便说一下,在您的脚本中,我认为即使将
const response=UrlFetchApp.fetch(####)
的const
修改为var
,也不会发生错误
当上述要点反映到脚本中时,下面的修改如何
模式1:
在此模式中,使用for循环和修改的exportOptions
。在这种情况下,每个工作表都作为每个附件文件添加。因此,当电子表格中有3个工作表时,3个PDF文件作为附件文件发送
请修改如下
发件人:
如果我误解了你的问题,并且这不是你想要的方向,我很抱歉。UrlFetchApp.fetch
是异步的。(这意味着它在代码流的“外部”工作,因此你在响应时会得到一个“未定义的”,因为它没有完全初始化某些请求)您需要一个处理程序来等待响应完成。请参阅-您将看到一个。然后部分-这发生在AJAX调用之后,并确保在尝试进一步操作之前完成请求。
const exportOptions =
'exportFormat=pdf&format=pdf'
'&size=A4'
'&portrait=true' + // orientation, false for landscape
'&fitw=true&source=labnol' + // fit to page width, false for actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid='; // the sheet's Id
const token = ScriptApp.getOAuthToken();
const sheets = ss.getSheets();
// make an empty array to hold your fetched blobs
const blobs = [];
for (var i = 0; i < sheets.length; i += 1) {
// Convert individual worksheets to PDF
const response = UrlFetchApp.fetch(url + exportOptions + sheets[i].getSheetId(), {
headers: {
Authorization: 'Bearer ' + token
}
})
// convert the response to a blob and store in our array
blobs[i] = response.getBlob().setName('Daily Sales');
}
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [response]
});
const exportOptions =
'exportFormat=pdf&format=pdf' +
'&size=A4' +
'&portrait=true' + // orientation, false for landscape
'&fitw=true&source=labnol' + // fit to page width, false for actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid='; // the sheet's Id
const token = ScriptApp.getOAuthToken();
const sheets = ss.getSheets();
const blobs = [];
for (var i = 0; i < sheets.length; i += 1) {
const response = UrlFetchApp.fetch(url + exportOptions + sheets[i].getSheetId(), {headers: {Authorization: 'Bearer ' + token}});
blobs[i] = response.getBlob().setName('Daily Sales' + i);
}
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: blobs});
const exportOptions =
'exportFormat=pdf&format=pdf' +
'&size=A4' +
'&portrait=true' + // orientation, false for landscape
'&fitw=true&source=labnol' + // fit to page width, false for actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' // do not repeat row headers (frozen rows) on each page
const token = ScriptApp.getOAuthToken();
const response = UrlFetchApp.fetch(url + exportOptions, {headers: {Authorization: 'Bearer ' + token}});
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: [response.getBlob()]});