I receive an XLSX file from our client on regular basis, and I would like to automate the process of importing it from Gmail (it's automatically labeled) into Google Sheets. So far I managed to make it work for CSV files, but XLSX files seem to be trickier. Can someone help to adjust this code I have for CSV files?
function getCSV()
{
var thread = GmailApp.getUserLabelByName(‘Reconciliation’).getThreads(0,1);
var messages = thread[0].getMessages();
var len = messages.length;
var message=messages[len-1] //get last message
var attachments = message.getAttachments(); // Get attachment of first message
var csv = attachments[0].getDataAsString();
var data = Utilities.parseCsv(csv);
var sheet = SpreadsheetApp.openById("some id").getSheetByName(‘Data’);
sheet.clearContents();
var range = sheet.getRange(1, 1, data.length, data[0].length);
range.setValues(data);
}
If my understanding is correct, how about this modification? Please think of this as just one of several answers.
When you use this script, please enable Drive API at Advanced Google Services and API console. You can see about this at here.
Flow of modified script:
Data
in the existing Spreadsheet.Modified script:
Please modify as follows.
From: To:Note:
attachments[0]
is the blob of xlsx file.Reference:
If I misunderstood your question and this didn't work, I apologzize.