I have a google sheet with Order no in Column A.
I receive a mail from one user in which he sends me the report about all orders. The Order no is column A of the Excel file. The Excel file has 5 such sheets.
When I receive the mail from the user, I will click the "Update" option in the user defined menu within my master google sheet and I want a function to run. That function will
- Copy the excel file to my drive in a given folder,
- Convert the file to google sheet,
- Open the google sheet,
getDataRange().getValues()
of the 5 sheets one by one.- Match the order no in master sheet
- Update the column B in MAster sheet by
setVale()
- Delete the excel file
- Delete the newly created google sheet file
If I can open the excel sheet, I will do the processing. I am not able to open the excel sheet by app script to read the data. I am running the script from the master google sheet (bound script). I will click the menu item when I receive a mail.
The existing solutions are getting the attachment blob, copy to drive and converting to google sheet. These solutions are not working. Maybe it is possible now to read the excel file directly without conversion with the recent improvement in google sheet.
var query = 'from:qad@megawinswitchgear.com subject:QAD Daily report filename=xlsx in:inbox has:nouserlabels' ;
var threads = GmailApp.search(query);
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
var blob = attachment.getAs(attachment.getContentType());
blob.setName(attachment.getName())
var excel = DriveApp.createFile(blob);
Drive.Files.copy({mimeType: MimeType.GOOGLE_SHEETS}, excel.getId());
excel.setTrashed(true)
Error for the above code
GoogleJsonResponseException: API call to drive.files.copy failed with error: The requested conversion is not supported.
var query = 'from:qad@megawinswitchgear.com subject:QAD Daily report filename=xlsx in:inbox has:nouserlabels' ;
var threads = GmailApp.search(query);
var messages = threads[0].getMessages();
var message=messages[0] //get first message
var attachments = message.getAttachments(); // Get attachment of first message
var xlsxBlob = attachments[0]; // Is supposes that attachments[0] is the blob of xlsx file.
var convertedSpreadsheetId = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS}, xlsxBlob).id;
var sheet = SpreadsheetApp.openById(convertedSpreadsheetId).getSheets()[1]; // There is the data in 1st tab.
var data = sheet.getDataRange().getValues();
Drive.Files.remove(convertedSpreadsheetId); // Remove the converted file.
sheet.clearContents();
var range = sheet.getRange(1, 1, data.length, data[0].length);
range.setValues(data);
thread[0].addLabel("QAD");
Error for the above code:
Exception: Unexpected error while getting the method or property openById on object SpreadsheetApp
Any help will be appreciated