0

I have a short code snippet that should convert an Excel file to Google Sheet via App Script on Google Drive. That uses DriveApp service and inserts a new file with mimeType : MimeType.GOOGLE_SHEETS

function convertExcelFileToGoogleSheets(fileId){

  let excelFile = DriveApp.getFileById(fileId);
  if(excelFile == null) return -1;
  if(excelFile.getMimeType == MimeType.Google_Sheets) return fileId;

  let blob = excelFile.getBlob();
  let config = {
    title : "[Google Sheets]" + excelFile.getName(),
    parents : [{id: excelFile.getParents().next().getId()}],
    mimeType : MimeType.GOOGLE_SHEETS
  };
  let spreadSheet = Drive.Files.insert(config, blob);

  return spreadSheet.id;
}

But it converts to a new file with Google Docs instead of expected Google Sheet

result of converted

Here is the Google Enum MimeType of AppScript Documentation.

Amit Dash
  • 584
  • 8
  • 21
ComaGear
  • 3
  • 2
  • Welcome to [so]. The "Excel" file has the wrong icon and doesn't show the file extension.... What is the extension of the file? – Rubén Aug 01 '22 at 16:15
  • Thanks Ruben, I just noted that truely problem of my code already. I am gonna convert google sheet to google sheet. so getting another mime type of file. – ComaGear Aug 01 '22 at 16:25
  • I have change target file to Truely excel file and convert. it was not problem. – ComaGear Aug 01 '22 at 16:26
  • 2
    I suppose here is the working code: https://stackoverflow.com/questions/56063156/script-to-convert-xlsx-to-google-sheet-and-move-converted-file probably you need to change: `let spreadSheet = Drive.Files.insert(config, blob);` to: `let spreadSheet = Drive.Files.insert(config, blob, {convert: true});` – Yuri Khristich Aug 01 '22 at 16:27
  • 1
    As an additional note, I think `if(excelFile.getMimeType == MimeType.Google_Sheets) return fileId;` should be `if(excelFile.getMimeType() == MimeType.GOOGLE_SHEETS) return fileId;` – Rafa Guillermo Aug 02 '22 at 07:36

0 Answers0