目前正在嘗試制作一個自動化發票電子郵件的腳本。該腳本從 Google 電子表格中獲取資訊并填充 Google 表單。然后將表格轉換為 PDF 并通過電子郵件發送。我一直遇到同樣的錯誤。
我的編程知識非常基礎,腳本本身完全取自視頻資源。
const TEMPLATE_FILE_ID = '1R-EX_iu418HrG8dTpS0MU04okzHa4-qQOd3b5ggvZKY';
const DESTINATION_FOLDER_ID = '1D4DlsrLEClE4wfHHu0FK3tQP84ZLirl4';
const CURRENCY_SIGN = 'Php.';
const PDF_FOLDER_ID = DriveApp.getFolderById("1q1DvS9-48oGaY8j5bWy2sgiR4SzRclEa");
function toCurrency(num) {
var fmt = Number(num).toFixed(2);
return `${CURRENCY_SIGN}${fmt}`;
}
function toDateFmt(dt_string) {
var millis = Date.parse(dt_string);
var date = new Date(millis);
var year = date.getFullYear();
var month = ("0" (date.getMonth() 1)).slice(-2);
var day = ("0" date.getDate()).slice(-2);
return `${year}-${month}-${day}`;
}
function parseFormData(values, header) {
var subtotal = 0;
var discount = 0;
var response_data = {};
for (var i = 0; i < values.length; i ) {
var key = header[i];
var value = values[i];
if (key.toLowerCase().includes("price")) {
subtotal = value;
value = toCurrency(value);
} else if (key.toLowerCase().includes("discount")) {
discount = value;
value = toCurrency(value);
} else if (key.toLowerCase().includes("date")) {
value = toDateFmt(value);
}
response_data[key] = value;
}
response_data["sub_total"] = toCurrency(subtotal);
response_data["total"] = toCurrency(subtotal - discount);
return response_data;
}
function populateTemplate(document, response_data) {
var document_header = document.getHeader();
var document_body = document.getBody();
for (var key in response_data) {
var match_text = `{{${key}}}`;
var value = response_data[key];
document_header.replaceText(match_text, value);
document_body.replaceText(match_text, value);
}
}
function createPDF(response_data) {
var sheet = SpreadsheetApp.getActiveSheet();
var last_row = sheet.getLastRow() - 1;
var range = sheet.getDataRange();
var data = range.getValues()[last_row];
var headers = range.getValues()[0];
var response_data = parseFormData(data, headers);
const template_file = DriveApp.getFileById(TEMPLATE_FILE_ID);
const target_folder = DriveApp.getFolderById(DESTINATION_FOLDER_ID);
var filename = `${response_data["Invoice Date"]}_${response_data["Company Name"]}_${response_data["Invoice Number"]}`;
const newTempDoc = template_file.makeCopy(filename, target_folder);
var document = DocumentApp.openById(newTempDoc.getId());
populateTemplate(document, response_data);
document.saveAndClose();
const blobPDF = newTempDoc.getAs(MimeType.PDF);
const pdfFile = PDF_FOLDER_ID.createFile(blobPDF).setName(response_data["Company Name"] "_Invoice");
target_folder.removeFile(newTempDoc);
GmailApp.sendEmail(toString(response_data["Email"]), "My Company Invoice", "Attached is your invoice. Have a good day!", {attachments: pdfFile, name: '"My Company Name"'});
}
我的問題顯然在最后一行
GmailApp.sendEmail(toString(response_data["Email"]), "My Company Invoice", "Attached is your invoice. Have a good day!", {attachments: pdfFile, name: '"My Company Name"'});
我試圖將電子郵件轉換為字串,但這導致了標題中顯示的錯誤。在這一點上,我歡迎任何建議。
uj5u.com熱心網友回復:
考慮到response_data["Email"]正在回傳一個有效的電子郵件。
嘗試替換這個:
GmailApp.sendEmail(toString(response_data["Email"]), "My Company Invoice", "Attached is your invoice. Have a good day!", {attachments: pdfFile, name: '"My Company Name"'});
有了這個 :
GmailApp.sendEmail(response_data["Email"].toString(), "My Company Invoice", "Attached is your invoice. Have a good day!", {attachments: pdfFile, name: '"My Company Name"'});
參考:
發送電子郵件
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/398380.html
標籤:谷歌应用程序脚本
