我正在尝试编写一个脚本,当一个特定单元格不为空时发送一张纸的内容。 脚本的检查部分正在运行。电子邮件部分也可以正常工作。我想将工作表的内容(最好是 xlsx 格式,但如果更简单的话 csv 也可以)附加到电子邮件中,但我在网上找到的所有脚本似乎都使用一些 Google Drive API,并且不知道如何使用它。
我注释掉了应该进行导出的行,目前,脚本只是发送提醒以手动导出工作表。我想将其自动化。 另外,我不想将电子邮件发送到硬编码电子邮件,而是想将其发送到同一张表 (I1) 中特定单元格中找到的电子邮件地址。
如有任何帮助,我们将不胜感激。
这是我的脚本:
unction checkAndExport() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = 'Bookings List'; // Replace with the desired sheet name
var sheet = spreadsheet.getSheetByName(sheetName);
var checkCell = sheet.getRange('B3'); // Cell to check if it is empty
var cellValue = checkCell.getValue();
if (cellValue !== '') {
// Export sheet data to a CSV string
// var outputDocument = DocumentApp.create('My custom csv file name');
// var content = getCsv();
// var textContent = ContentService.createTextOutput(content);
// textContent.setMimeType(ContentService.MimeType.CSV);
// var blob = Utilities.newBlob(textContent, 'text/csv', sheetName + '.csv');
// Send an email with the CSV file attached
var recipientEmail = '[email protected]'; // Replace with actual email address
var subject = 'Bookings Email reminder';
var body = 'It is time to send the bookings list to some leader!';
var options = {
name: 'Exported Sheet',
// mimeType: blob.getContentType(),
inline: false,
// attachments: [blob]
};
从您的问题来看,我无法理解您想要附加为 XLSX 数据的来源。所以,这是我的猜测。为了将活动电子表格中的特定工作表作为 XLSX 数据附加到电子邮件中,请进行以下修改如何?
function checkAndExport() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = 'Bookings List'; // Replace with the desired sheet name
var sheet = spreadsheet.getSheetByName(sheetName);
var checkCell = sheet.getRange('B3'); // Cell to check if it is empty
var cellValue = checkCell.getValue();
if (cellValue !== '') {
// --- I modified the below script.
// Retrieve XLSX data from the specific sheet of the active Spreadsheet as blob.
var format = "xlsx"; // or "csv"
var sheetId = sheet.getSheetId(); // or spreadsheet.getSheetByName("sheet name").getSheetId()
var blob = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/export?id=${spreadsheet.getId()}&exportFormat=${format}&gid=${sheetId}`, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob();
// Send an email with the CSV file attached
var recipientEmail = sheet.getRange("I1").getDisplayValue();
var subject = 'Bookings Email reminder';
var body = 'It is time to send the bookings list to some leader!';
MailApp.sendEmail({ to: recipientEmail, subject, body, name: 'Exported Sheet', attachments: [blob] });
}
}
var sheetId = sheet.getSheetId();
修改为var sheetId = spreadsheet.getSheetByName("sheet name").getSheetId();
。var format = "xlsx";
修改为 var format = "csv";
。Also, instead of sending the email to a hard-coded email, I would like to send it to the email address found in a particular cell in the same sheet (I1).
。MailApp.sendEmail
作为示例添加了它。所以,请根据您的实际情况进行修改。