1

the code works fine with another email attachment
" var convertedSpreadsheet = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS}, attachment); " however when i changed it to type Microsoft_Excel it started to give me the error. can it be the problem?

// Get the sheets and ranges to use
var marketsSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("markets");
var marketsRange = marketsSheet.getDataRange();
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Magnite Raw Data");
// Clear the existing data in range A2:L of the "Magnite Raw Data" sheet
dataSheet.getRange("A2:M").clearContent();
// Get the values from the markets range and remove any empty rows
var marketsValues = marketsRange.getValues().filter(row => row[0] !== '');
// Loop through the markets values and process each one
for (var i = 0; i < marketsValues.length; i++) {
var subject = marketsValues[i][0];
// Search for emails with the given subject
var threads = GmailApp.search('from: reports-delivery@magnite.com "subject:' + subject + '" in:PMP Reports'); // "subject contains"
if (threads.length === 0) {
console.log('No emails found with subject: ' + subject);
continue;
    }
// Download the first attachment of the first message found
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
if (!attachment) {
console.log('No attachments found in email with subject: ' + subject);
continue;
    }
// Convert the attachment to a Google Sheets file and get the data
var convertedSpreadsheet = Drive.Files.insert({mimeType: MimeType.MICROSOFT_EXCEL}, attachment);
var convertedSheet = SpreadsheetApp.openById(convertedSpreadsheet.id).getSheetByName("Report");
var data = convertedSheet.getDataRange().getValues();
// Find the last row with data in column A of the data sheet
var lastRow = dataSheet.getRange("A:A").getValues().filter(String).length + 1;
// Paste the data into the data sheet starting at the last row found
var dataRange = dataSheet.getRange(lastRow, 1, data.length, data[0].length);
dataRange.setValues(data);
// Delete the converted spreadsheet
Drive.Files.remove(convertedSpreadsheet.id);
  }
// Delete rows containing "Total" or "Date" in column A of the "Magnite Raw Data" sheet
var dataRange = dataSheet.getRange("A2:M" + dataSheet.getLastRow());
var values = dataRange.getValues();
var newValues = values.filter(function(row) {
return row[0].indexOf("Total") === -1 && row[0].indexOf("Date") === -1;
  });
dataRange.clearContent();
dataRange.offset(0, 0, newValues.length, newValues[0].length).setValues(newValues);
// Add "Magnite" to column M to column N of the "Magnite Raw Data" sheet
var lastRowIndex = dataSheet.getLastRow();
var range = dataSheet.getRange("M2:M" + lastRowIndex);
range.offset(0, 0, range.getNumRows(), 1).setValue("Magnite");
}  ```
Pelin
  • 127
  • 6
  • About `the code works fine with another email attachment " var convertedSpreadsheet = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS}, attachment); " however when i changed it to type Microsoft_Excel it started to give me the error.`, in your situation, what is each mimeType of the original data of `the code works fine with another email attachment` and `however when i changed it to type Microsoft_Excel it started to give me the error.`? – Tanaike Apr 19 '23 at 00:09
  • hey @pelin is this still an issue for you? or did Rene's answer help in some way? having kinda the same issue here – Yancy Godoy Apr 25 '23 at 21:00

1 Answers1

0

The reason why you are getting that error is that the line

var convertedSpreadsheet = Drive.Files.insert({mimeType: MimeType.MICROSOFT_EXCEL}, attachment);

is meant to create a file and then this file's ID is used on

var convertedSheet = SpreadsheetApp.openById(convertedSpreadsheet.id).getSheetByName("Report");

Since the file is no longer a google spreadsheet then the SpreadsheetApp Class is unable to read the file's format.

I don't understand the context and would be happy to provide a better answer if you could explain why the change of mimeType was made but if you were trying to open an excel file you might want to change the convertedSheet variable to include the convert parameter like so:

var convertedSpreadsheet = Drive.Files.insert({mimeType: MimeType.MICROSOFT_EXCEL}, attachment).Convert

Feel free to comment and let me know more about the mimeType change in case the suggestion does not work.

Best of luck!

Rene Olivo
  • 526
  • 1
  • 10
  • Thanks for the answer. The reason i made the change was that altough the same code works for different emails with a different senders -all xlsx attachments- it doesnt work for this one,for a reason i dont understand.So i asked chatgpt and it told me this would work therefore i made the change - i know not a good reason but I am quite a beginner so I just went with it... :) below is the error btw i get when i run it with " Google_sheets mime type" **"GoogleJsonResponseException: API call to drive.files.insert failed with error: Invalid mime type provided Magnite_Iterate @ Magnite.gs:35"** – Pelin Apr 19 '23 at 08:18
  • That's a very interesting behavior! I hope the change works for you and that you don't come across that or a similar error in the future however if it does happen [this post](https://stackoverflow.com/questions/60693366/xlsx-from-gmail-to-google-sheets-invalid-mime-type-content-type-seems-to-be-ap) might help, best of luck! – Rene Olivo Apr 19 '23 at 12:50
  • @Pelin if Rene's answer actually worked for you (at least for your initial concern), don't forget to either upvote or mark it as 'best answer'. By doing so, other people in the community, who may have the same concern as you, will know that theirs can be resolved. – Lorena Gomez Apr 19 '23 at 12:59
  • it actually didnt work. it is giving me the error **TypeError: Cannot read properties of undefined (reading 'id') Magnite_Iterate @ Magnite.gs:39** – Pelin Apr 19 '23 at 14:44
  • I took a second look at the code and since it was working correctly at the beginning I wonder if the reason why the Invalid mime type provided error message showed in the first place is because that particular email had more than a single attachment or if the attachment type is somewhat wrong, would it be possible for you to share a cleaned up version of the raw message? You should be able to get that info by following [these steps](https://support.google.com/mail/answer/29436) if you can't, could you please check if the raw email contains the MimeType *"application/vnd.openxmlformats"?* – Rene Olivo Apr 19 '23 at 19:23