Tengo una función que me permite importar datos de hojas de cálculo de mi Gmail a Hojas de cálculo de Google. Anteriormente, los datos de la hoja de cálculo solo tenían 6 columnas para importar. Ahora, se realizaron algunos cambios nuevos y se agregó una séptima columna. Después de implementar este cambio, mi función ya no funciona y Google Sheets arroja este error. ¿Puedo por favor tener alguna ayuda?
Entonces, mientras veo esto, la funcionalidad prevista me parece correcta. Omita las primeras 3 filas ( netdata ) y tome todo a continuación. ¿Podría ser el + 1, 1 ?
El error:
TypeError: Cannot read property 'getRange' of nullMi función de importación:
function importCSVFromGmail() { var sheetName = "SHEET_NAME"; // Name of sheet tab. var threads = GmailApp.search("from:EMAIL HERE label:LABEL HERE"); // "from:recipient email here label:name of your filter/label here" var messages = threads[0].getMessages(); var message = messages[messages.length - 1]; var attachment = message.getAttachments()[0]; // [0] will by default look for only 1 attachment. If there are more than two attachment increase value. ex: [1] , [2] var data = []; if (attachment.getContentType() == MimeType.CSV) { // This will look for a CSV file type first data = Utilities.parseCsv(attachment.getDataAsString(), ","); } else if (attachment.getContentType() == MimeType.MICROSOFT_EXCEL || attachment.getContentType() == MimeType.MICROSOFT_EXCEL_LEGACY) { // If attachment is an xls, this line will look at the content to determine and convert accordingly. var tempFile = Drive.Files.insert({title: "temp", mimeType: MimeType.GOOGLE_SHEETS}, attachment).id; data = SpreadsheetApp.openById(tempFile).getSheets()[0].getDataRange().getValues(); Drive.Files.trash(tempFile); } if (data.length > 0) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); var netdata = data.slice(3); // This will skip the number of rows starting from the top. sheet.getRange(sheet.getLastRow() + 1, 1, netdata.length, netdata[0].length).setValues(netdata ); } }Creo que el problema está aquí:
sheet.getRange(sheet.getLastRow() + 1, 1, netdata.length, netdata[0].length).setValues(netdata );El mensaje de error indica que el problema es que la variable sheet es nula. Eso sucede cuando no hay una hoja con el nombre SHEET_NAME en la hoja de cálculo.
Para corregir el error, reemplace SHEET_NAME con el nombre de la hoja con la que desea que trabaje la función. Compruebe cosas como espacios en blanco iniciales y finales en el nombre de la hoja.