0

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

  1. Copy the excel file to my drive in a given folder,
  2. Convert the file to google sheet,
  3. Open the google sheet,
  4. getDataRange().getValues() of the 5 sheets one by one.
  5. Match the order no in master sheet
  6. Update the column B in MAster sheet by setVale()
  7. Delete the excel file
  8. 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

Rubén
  • 34,714
  • 9
  • 70
  • 166
arul selvan
  • 616
  • 4
  • 17
  • 2
    The first method is not supported, but are you sure that the attachments[0] is the file you are looking for? The second code sample is working fine for me. – Alessandro Nov 02 '20 at 12:06
  • What do you mean by XLS? Are you using it as a short name for Excel or the file format is actual XLS? Also please add a brief description of your search / research efforts as is suggested in [ask]. [\[google-apps-script\] convert excel](https://stackoverflow.com/search?q=%5Bgoogle-apps-script%5D+convert+excel) returns 187 results – Rubén Nov 02 '20 at 16:56
  • I receive an Excel file (extension XLSX). I want to get values of each sheet and each row. Depending on the value in one column, I will update my master file by finding a matching value in the master – arul selvan Nov 03 '20 at 03:49
  • I have to apologize for my poor English skill. I can understand about `I want to getValues() of each sheet and each row.`. But unfortunately, I cannot understand about `Depending on the value in one column, I will update my master file by finding a matching value in the master in a particular column.`. Can I ask you about the detail of it? By the way, `var attachment = message.getAttachments()[0];` and `var xlsxBlob = attachments[0];` are always the XLSX data. Is my understanding correct? – Tanaike Nov 03 '20 at 04:59
  • I get an email daily from one email ID. It has an XLSX attachment. I want to click a menu item from my google sheet. On clicking the menu item, I want to open the excel file and getDataRange().getValues in each sheet one by one. I will do all other steps myself. I just need help to open the XLSX file within app script – arul selvan Nov 04 '20 at 10:37
  • @arul selvan Thank you for replying. I noticed your replying just now. I apologize for this. Unfortunately, I couldn't understand about `I just need help to open the XLSX file within app script`. This is due to my poor English skill. I deeply apologize for this. But when I could correctly understand about your goal and see the vision of it, I would like to think of the solution. By the way, when `@` is added to the user's name when you replying, the user can notice the replying. – Tanaike Nov 04 '20 at 23:10
  • @Tanaike : 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 and I want a function to run. That function will 1.Copy the excel file to my drive in a given folder, 2.Convert the file to google sheet, 3. Open the google sheet, 4.getDataRange().getValues() of the 5 sheets one by one. 5.Match the order no in master sheet 6.Update by setVale() – arul selvan Nov 06 '20 at 03:48
  • @arul selvan Thank you for replying. Unfortunately, I cannot still understand about your situation and goal. This is due to my poor English skill. I apologize for this again. But when I could correctly understand about your goal and see the vision of it, I would like to think of the solution. I deeply apologize that I cannot resolve your question soon. – Tanaike Nov 06 '20 at 07:10
  • 1
    Related [XLSX from Gmail to Google Sheets: invalid mime type. Content type seems to be application/octet?](https://stackoverflow.com/q/60693366/1595451), [Convert excel files to Google Spreadsheet and replace existing spreadsheet files automatically](https://stackoverflow.com/q/55453479/1595451) – Rubén Nov 15 '20 at 04:46

0 Answers0