0

The Code throws the error as GoogleJsonResponseException: API call to drive.files.insert failed with error: Invalid mime type provided. But the code works fine for some piece of data but if the data is upto 15mb it throws the above error. Please do help me with the same. Even i have tried using enabling Drive API.

function getCSV() 
{
  //var thread = GmailApp.getUserLabelByName('BalancePayment_Orders_Delivered2022-05-20').getThread(0,1);
  var thread = GmailApp.search('in:inbox subject:"BalancePayment_Orders_Delivered2022-05-20"');
  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 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()[0]; // There is the data in 1st tab.
  var data = sheet.getDataRange().getValues();
  Drive.Files.remove(convertedSpreadsheetId); // Remove the converted file.

  var sheet = SpreadsheetApp.openById("1MSOE90X8XVfns2TloqItKJ8KnGDFhTtLEKDByl3iH8Y").getSheetByName("Sales");
  sheet.clearContents();
  var range = sheet.getRange(1, 1, data.length, data[0].length);
  range.setValues(data);
}

Rubén
  • 34,714
  • 9
  • 70
  • 166

0 Answers0