Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Recupere un archivo CSV recibido a través de Gmail desde una dirección de correo electrónico específica a una hoja de cálculo de Google específica

Objetivos del problema:

  • Recupera el mensaje de Gmail usando la dirección de correo electrónico.
  • Recupere los archivos CSV de los archivos adjuntos y colóquelos en una hoja de cálculo de Google. Elimine las 2 primeras filas de los datos CSV. (Elimine los datos anteriores en Google Sheets y actualícelos con los nuevos datos CSV cada vez que los reciba de la dirección de correo electrónico específica).
  • Logre esto usando Google Apps Script.

Ya lo intenté: automatice un archivo CSV recibido a través de Gmail desde una identificación de correo electrónico específica a una hoja de cálculo de Google específica

Pero nada está resolviendo los 3 problemas anteriores en específico.

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

Intenté el código anterior pero muestra un error Excepción: Argumento no válido: en myFunction (Código: 8:28)

FYI, también quiero usar ID de hoja en lugar de Nombre de hoja

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Intente (coloque el ID de la hoja de cálculo, el asunto -si tiene varias palabras, especifique como en el ejemplo a continuación- y el correo electrónico del remitente a continuación):

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

referencias

GmailApp.búsqueda

sintaxis de búsqueda

obtener archivos adjuntos ()

Utilidades.parseCsv

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!