检索通过 Gmail 从特定电子邮件地址接收到特定 Google Sheet 的 CSV 文件
Retrieve a CSV file received over Gmail from a specific email address to a specific Google Sheet
问题的目标:
- 使用电子邮件地址从 Gmail 中检索邮件。
- 从附件文件中检索 CSV 文件并将它们放在 Google Spreadsheet 中的 sheet 上。从 CSV 数据中删除 1st 2 行。 (删除 Google Sheet 上的过去数据,并在从特定电子邮件地址收到时用新的 CSV 数据更新它。
- 使用 Google Apps 脚本实现此目的。
已经尝试过:Automate a CSV file received over Gmail from a specific email id to a specific Google Sheet
但是没有具体解决以上3个问题
function myFunction() {
const messageId = "###"; // Please set the message ID of Gmail.
const sheetName = "Sheet1"; // Please set the sheet name you want to put the values.
const delimiter = ","; // If your CSV data uses the specific delimiter, please set this.
const skipRows = 2; // 2 is from your question.
// 1. Retrieve message.
const message = GmailApp.getMessageById(messageId);
// 2. Retrieve attachment files.
const attachments = message.getAttachments();
if (attachments.length == 0) {
console.log("No attachment files.");
return;
}
// 3. Create an array for putting to Spreadsheet from the CSV data of attachment files.
const values = attachments.reduce((ar, e) => {
if (e.getContentType() == MimeType.CSV || e.getName().includes(".csv")) {
ar = [...ar, ...Utilities.parseCsv(e.getDataAsString(), delimiter).splice(skipRows)];
}
return ar;
}, []);
if (values.length == 0) {
console.log("No values.");
return;
}
// 4. Put the values to Spreadsheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); // and, you can also use. const sheet = SpreadsheetApp.openById("###spreadsheetId###").getSheetByName(sheetName);
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
尝试了上面的代码,但它显示错误 异常:参数无效:在 myFunction(代码:8:28)
仅供参考,我还想使用 Sheet ID 而不是 Sheet 名称
尝试(输入电子表格 ID、主题 - 如果您有多个单词,请在下面的示例中指定 - 以及下面的发件人电子邮件):
const getGmailAttachment = () => {
const ssID = '############'
const searchQuery = 'from:######@gmail.com in:inbox has:attachment subject:##### subject:###';
const threads = GmailApp.search(searchQuery, 0, 1);
threads.forEach(thread => {
const message = thread.getMessages()[Number(thread.getMessageCount() - 1)];
const attachments = message.getAttachments();
attachments.forEach((attachment, i) => {
if (i == 0) {
console.log(attachment.getName())
const sheet = SpreadsheetApp.openById(ssID).getSheets()[0];
sheet.getDataRange().clearContent()
const csvData = Utilities.parseCsv(attachment.getDataAsString()).splice(2); // except 2 first rows
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
});
});
};
参考文献
问题的目标:
- 使用电子邮件地址从 Gmail 中检索邮件。
- 从附件文件中检索 CSV 文件并将它们放在 Google Spreadsheet 中的 sheet 上。从 CSV 数据中删除 1st 2 行。 (删除 Google Sheet 上的过去数据,并在从特定电子邮件地址收到时用新的 CSV 数据更新它。
- 使用 Google Apps 脚本实现此目的。
已经尝试过:Automate a CSV file received over Gmail from a specific email id to a specific Google Sheet
但是没有具体解决以上3个问题
function myFunction() {
const messageId = "###"; // Please set the message ID of Gmail.
const sheetName = "Sheet1"; // Please set the sheet name you want to put the values.
const delimiter = ","; // If your CSV data uses the specific delimiter, please set this.
const skipRows = 2; // 2 is from your question.
// 1. Retrieve message.
const message = GmailApp.getMessageById(messageId);
// 2. Retrieve attachment files.
const attachments = message.getAttachments();
if (attachments.length == 0) {
console.log("No attachment files.");
return;
}
// 3. Create an array for putting to Spreadsheet from the CSV data of attachment files.
const values = attachments.reduce((ar, e) => {
if (e.getContentType() == MimeType.CSV || e.getName().includes(".csv")) {
ar = [...ar, ...Utilities.parseCsv(e.getDataAsString(), delimiter).splice(skipRows)];
}
return ar;
}, []);
if (values.length == 0) {
console.log("No values.");
return;
}
// 4. Put the values to Spreadsheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); // and, you can also use. const sheet = SpreadsheetApp.openById("###spreadsheetId###").getSheetByName(sheetName);
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
尝试了上面的代码,但它显示错误 异常:参数无效:在 myFunction(代码:8:28)
仅供参考,我还想使用 Sheet ID 而不是 Sheet 名称
尝试(输入电子表格 ID、主题 - 如果您有多个单词,请在下面的示例中指定 - 以及下面的发件人电子邮件):
const getGmailAttachment = () => {
const ssID = '############'
const searchQuery = 'from:######@gmail.com in:inbox has:attachment subject:##### subject:###';
const threads = GmailApp.search(searchQuery, 0, 1);
threads.forEach(thread => {
const message = thread.getMessages()[Number(thread.getMessageCount() - 1)];
const attachments = message.getAttachments();
attachments.forEach((attachment, i) => {
if (i == 0) {
console.log(attachment.getName())
const sheet = SpreadsheetApp.openById(ssID).getSheets()[0];
sheet.getDataRange().clearContent()
const csvData = Utilities.parseCsv(attachment.getDataAsString()).splice(2); // except 2 first rows
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
});
});
};