需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

huangapple go评论73阅读模式
英文:

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 = &quot;Report Data&quot;; // Replace with your desired label
var subjects = [&quot;Data 1&quot;, &quot;Data 2&quot;, &quot;Data 3&quot;]; // Replace with your desired subjects
var spreadsheetId = &quot;12345678900987654311&quot;; // Replace with your Google Sheets file ID
// Import CSV data into each sheet
for (var i = 0; i &lt; 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(&#39;label:&#39; + label + &#39; subject:&#39; + subject);
// Retrieve the CSV attachment from the email
for (var j = 0; j &lt; threads.length; j++) {
var thread = threads[j];
var messages = thread.getMessages();
for (var k = 0; k &lt; messages.length; k++) {
var message = messages[k];
var attachments = message.getAttachmentsByType(&#39;application/csv&#39;);
if (attachments.length &gt; 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()以获取您想要的结果。

脚本的运行如下所示:

  1. 清除具有相同主题名称的表的现有内容。
  2. 搜索具有所需主题的电子邮件。
  3. 如果电子邮件存在,则获取消息以及附件。
  4. 如果附件为text/csv,则将内容粘贴到具有相同主题名称的表中。

修改后的脚本:

function ExtractCSVFromGmailAndImport() {
  var subjects = [&quot;Data 1&quot;, &quot;Data 2&quot;, &quot;Data 3&quot;]; // 用您想要的主题替换
  var spreadsheetId = &quot;1234545&quot;; // 用您的 Google Sheets 文件 ID 替换
  
  // 将 CSV 数据导入到每个表中
  for (var i = 0; i &lt; subjects.length; i++) {
    var subject = subjects[i];
    var sheetName = subject;
    var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
    
    // 清除表中现有数据
    sheet.clear();
    
    // 获取 Gmail 标签
    var threads = GmailApp.search(&#39;subject:&#39; + subject);
    
    // 从电子邮件中检索 CSV 附件
    for (var j = 0; j &lt; threads.length; j++) {
      var thread = threads[j];
      var messages = thread.getMessages();
      
      for (var k = 0; k &lt; messages.length; k++) {
        var message = messages[k];
        var attachments = message.getAttachments()[k];
        if (attachments.getContentType() === &#39;text/csv&#39;) {
          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(&#39;未找到 CSV 文件。&#39;);
        }
      }
    }
  }
}

来自电子邮件的示例 CSV 文件:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

Data 1:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

Data 2:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

Data 3:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

输出:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

参考资料:

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:

  1. Clear the existing content of the sheets with the same subject name
  2. Search emails with the desired subject
  3. If emails exist,obtain the message along with attachments
  4. If an attachment is text/csv,paste the content to the sheet with its same subject name

Modified script:

function ExtractCSVFromGmailAndImport() {
var subjects = [&quot;Data 1&quot;, &quot;Data 2&quot;, &quot;Data 3&quot;]; // Replace with your desired subjects
var spreadsheetId = &quot;1234545&quot;; // Replace with your Google Sheets file ID
// Import CSV data into each sheet
for (var i = 0; i &lt; 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(&#39;subject:&#39; + subject);
// Retrieve the CSV attachment from the email
for (var j = 0; j &lt; threads.length; j++) {
var thread = threads[j];
var messages = thread.getMessages();
for (var k = 0; k &lt; messages.length; k++) {
var message = messages[k];
var attachments = message.getAttachments()[k];
if (attachments.getContentType() === &#39;text/csv&#39;) {
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(&#39;No CSV file found.&#39;);
}
}
}
}
}

Sample CSV file from email:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

Data 1:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

Data 2:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

Data 3:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

Output:

需要帮助导入Gmail附件中的CSV文件数据到现有的Google表格。

References:

https://developers.google.com/apps-script/reference/gmail/gmail-app

https://developers.google.com/apps-script/reference/gmail/gmail-message#getattachments

huangapple
  • 本文由 发表于 2023年5月17日 14:32:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定