简体   繁体   English

将通过 Gmail 从特定电子邮件地址接收到的 CSV 文件检索到特定的 Google 表格

[英]Retrieve a CSV file received over Gmail from a specific email address to a specific Google Sheet

Goals of the problem:问题的目标:

  • Retrieve the message from Gmail using the email address.使用电子邮件地址从 Gmail 中检索邮件。
  • Retrieve the CSV files from the attachment files and put them on a sheet in Google Spreadsheet.从附件文件中检索 CSV 文件并将它们放在 Google 电子表格中的工作表上。 Remove 1st 2 rows from the CSV data.从 CSV 数据中删除第一 2 行。 (Remove the past data on Google Sheets and update it with the new CSV data whenever received from the specific email address). (删除 Google 表格上的过去数据,并在收到来自特定电子邮件地址的新 CSV 数据时对其进行更新)。
  • Achieve this using Google Apps Script.使用 Google Apps 脚本实现此目的。

Already tried: Automate a CSV file received over Gmail from a specific email id to a specific Google Sheet已经尝试过: 将通过 Gmail 从特定电子邮件 ID 接收的 CSV 文件自动化到特定的 Google 表格

But nothing is solving the above 3 problems in specific.但没有什么能具体解决上述三个问题。

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);
}

Tried the above code but it shows an error Exception: Invalid argument: at myFunction(Code:8:28)尝试了上面的代码,但它显示错误异常:无效参数:在 myFunction(代码:8:28)

FYI, I also want to use Sheet ID instead of Sheet Name仅供参考,我也想使用工作表 ID 而不是工作表名称

Try (put the spreadsheet ID, the subject -if you have multiple words please specify as in the example hereafter- and the email of the sender hereafter):尝试(输入电子表格 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);
      }
    });
  });
};

references参考

GmailApp.search GmailApp.search

search syntax搜索语法

getAttachments() 获取附件()

Utilities.parseCsv 实用程序.parseCsv

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从gmail检索csv附件文件并将数据放在谷歌电子表格中 - Retrieve csv attachment file from gmail and place the data in a google spreadsheet 我可以使用Google Apps脚本通过Gmail重复发送电子邮件中的特定信息吗? - Can I use Google Apps Script to export specific information from a recurring email via Gmail? 如何使用应用脚本从 Gmail CSV 附件中提取特定列到 Google 电子表格? - How to pull specific columns from Gmail CSV attachment to Google Spreadsheet using app script? 有没有办法使用 html 和 js 按钮从特定的谷歌表格单元格中检索数据? - Is there a way to retrieve data from specific google sheet cells using html and js buttons? 从特定的邮件地址获取最近的电子邮件 - fetch recent email from a specific mail address 尝试从 Google 表格中的 URL 将文件的副本保存到特定的 Google Drive 文件夹 - Trying to save a copy of a file to specific Google Drive folder, from URLs in a Google Sheet imacros:如何从从.csv提取的电子邮件地址中删除后缀(@ gmail.com) - imacros: How to remove suffix (@gmail.com) from email address extracted from .csv 谷歌应用脚本从 gmail 中删除特定附件 - Google app Script delete specific attachment from gmail 需要从 Google Apps 脚本查询 Google 表格以获取特定数据 - Need to Query Google Sheet for specific data from Google Apps Script 如何从 Google 表格下载具有特定页边距和分页符的 PDF 文件? - How to download a PDF file from Google Sheet with specific margins and page breaks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM