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");
} ```