英文:
Need help in importing a CSV file data from Gmail attachment to an existing Google sheet
问题
Here is the translated code snippet:
我是这个群组中的新成员,对应用脚本毫无经验。
我在这里寻求帮助,开发一个可以从Gmail附件中导入CSV文件数据的脚本。我需要使用这些导入的数据每天生成报告。以下是情景:
CSV文件会按照计划自动生成并发送到我的Gmail收件箱。这些电子邮件可以通过“发件人电子邮件地址”和“主题”来识别。每封邮件只会有一个CSV文件附件。CSV附件的文件名相同,但作为单独的电子邮件以单独的主题发送。
我想将这些CSV附件导入到现有的Google表格中。根据邮件主题,每个CSV数据应该替换此Google表格的现有内容。
我在网络上和这个群组中都搜索了脚本,但没有成功。我正在发布一个我从网络上获取的脚本,但它不起作用。
function ExtractCSVFromGmailAndImport() {
var label = "报告数据"; // 用您希望的标签替换
var subjects = ["数据1", "数据2", "数据3"]; // 用您希望的主题替换
var spreadsheetId = "12345678900987654311"; // 用您的Google表格文件ID替换
// 将CSV数据导入每个工作表
for (var i = 0; i < subjects.length; i++) {
var subject = subjects[i];
var sheetName = subject;
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
// 清除工作表中的现有数据
sheet.clear();
// 获取Gmail标签
var threads = GmailApp.search('label:' + label + ' subject:' + subject);
// 从电子邮件中检索CSV附件
for (var j = 0; j < threads.length; j++) {
var thread = threads[j];
var messages = thread.getMessages();
for (var k = 0; k < messages.length; k++) {
var message = messages[k];
var attachments = message.getAttachmentsByType('application/csv');
if (attachments.length > 0) {
var attachment = attachments[0];
var csvData = Utilities.newBlob(attachment.getDataAsString()).getDataAsString();
var csv = Utilities.parseCsv(csvData);
sheet.getRange(sheet.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv);
}
}
}
}
}
请注意,这只是您提供的脚本的翻译部分,不包括完整的代码或错误修复。如果您需要帮助解决错误,请提供有关问题的更多信息。
英文:
I am new in this group, and I don't have any experience in apps scripts.
I am here asking for some help to develop a script which can import data from CSV file in Gmail attachment. I have to develop a report on daily basis using this imported data from the CSV file. Following are the scenarios.
The CSV files are automatically generated and received to my Gmail inbox on a scheduled time. Those emails can be identified by "sender email id" and "subject". Only one csv file attachment will be there in each mail. The CSV attachments are having same file name but are sent as separate email with separate subjects.
I want to import these csv attachments into an existing Gsheet. Based on mail subject, each csv data should replace the entire contents of the existing sheets of this Gsheet.
I searched web and even in this group for the script but was not successful. I am posting a script which I got from the web. But it's not working.
function ExtractCSVFromGmailAndImport() {
var label = "Report Data"; // Replace with your desired label
var subjects = ["Data 1", "Data 2", "Data 3"]; // Replace with your desired subjects
var spreadsheetId = "12345678900987654311"; // Replace with your Google Sheets file ID
// Import CSV data into each sheet
for (var i = 0; i < subjects.length; i++) {
var subject = subjects[i];
var sheetName = subject;
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
// Clear existing data in the sheet
sheet.clear();
// Get the Gmail label
var threads = GmailApp.search('label:' + label + ' subject:' + subject);
// Retrieve the CSV attachment from the email
for (var j = 0; j < threads.length; j++) {
var thread = threads[j];
var messages = thread.getMessages();
for (var k = 0; k < messages.length; k++) {
var message = messages[k];
var attachments = message.getAttachmentsByType('application/csv');
if (attachments.length > 0) {
var attachment = attachments[0];
var csvData = Utilities.newBlob(attachment.getDataAsString()).getDataAsString();
var csv = Utilities.parseCsv(csvData);
sheet.getRange(sheet.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv);
}
}
}
}
}
While running this script I am getting error as follows
Error
TypeError: message.getAttachmentsByType is not a function ExtractCSVFromGmailAndImport @ Code.gs:25
Kindly help me in this script.
答案1
得分: 1
错误是因为getAttachmentsByType()
不是GmailApp类中的方法。
我已经对您现有的脚本进行了调整,从使用getAttachmentsByType()
改为使用getAttachments()
以获取您想要的结果。
脚本的运行如下所示:
- 清除具有相同主题名称的表的现有内容。
- 搜索具有所需主题的电子邮件。
- 如果电子邮件存在,则获取消息以及附件。
- 如果附件为text/csv,则将内容粘贴到具有相同主题名称的表中。
修改后的脚本:
function ExtractCSVFromGmailAndImport() {
var subjects = ["Data 1", "Data 2", "Data 3"]; // 用您想要的主题替换
var spreadsheetId = "1234545"; // 用您的 Google Sheets 文件 ID 替换
// 将 CSV 数据导入到每个表中
for (var i = 0; i < subjects.length; i++) {
var subject = subjects[i];
var sheetName = subject;
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
// 清除表中现有数据
sheet.clear();
// 获取 Gmail 标签
var threads = GmailApp.search('subject:' + subject);
// 从电子邮件中检索 CSV 附件
for (var j = 0; j < threads.length; j++) {
var thread = threads[j];
var messages = thread.getMessages();
for (var k = 0; k < messages.length; k++) {
var message = messages[k];
var attachments = message.getAttachments()[k];
if (attachments.getContentType() === 'text/csv') {
var csvData = Utilities.newBlob(attachments.getDataAsString()).getDataAsString();
var csv = Utilities.parseCsv(csvData);
sheet.getRange(sheet.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv);
} else {
Logger.log('未找到 CSV 文件。');
}
}
}
}
}
来自电子邮件的示例 CSV 文件:
Data 1:
Data 2:
Data 3:
输出:
参考资料:
https://developers.google.com/apps-script/reference/gmail/gmail-app
https://developers.google.com/apps-script/reference/gmail/gmail-message#getattachments
英文:
The error occurs since getAttachmentsByType()
is not a method in Class GmailApp.
I have tweaked your existing script from using getAttachmentsByType()
to getAttachments()
to obtain your desired outcome.
The script works as follows:
- Clear the existing content of the sheets with the same subject name
- Search emails with the desired subject
- If emails exist,obtain the message along with attachments
- If an attachment is text/csv,paste the content to the sheet with its same subject name
Modified script:
function ExtractCSVFromGmailAndImport() {
var subjects = ["Data 1", "Data 2", "Data 3"]; // Replace with your desired subjects
var spreadsheetId = "1234545"; // Replace with your Google Sheets file ID
// Import CSV data into each sheet
for (var i = 0; i < subjects.length; i++) {
var subject = subjects[i];
var sheetName = subject;
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
// Clear existing data in the sheet
sheet.clear();
// Get the Gmail label
var threads = GmailApp.search('subject:' + subject);
// Retrieve the CSV attachment from the email
for (var j = 0; j < threads.length; j++) {
var thread = threads[j];
var messages = thread.getMessages();
for (var k = 0; k < messages.length; k++) {
var message = messages[k];
var attachments = message.getAttachments()[k];
if (attachments.getContentType() === 'text/csv') {
var csvData = Utilities.newBlob(attachments.getDataAsString()).getDataAsString();
var csv = Utilities.parseCsv(csvData);
sheet.getRange(sheet.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv);
} else {
Logger.log('No CSV file found.');
}
}
}
}
}
Sample CSV file from email:
Data 1:
Data 2:
Data 3:
Output:
References:
https://developers.google.com/apps-script/reference/gmail/gmail-app
https://developers.google.com/apps-script/reference/gmail/gmail-message#getattachments
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论