Objetivos del problema:
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
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); } }); }); };