What is the most efficient way to convert a .XLS to Google Sheets in App Script?

非 Y 不嫁゛ 提交于 2021-01-28 11:54:31

问题


I download a .XLS file automatically to a Google drive weekly. I would like to automatically convert the latest downloaded .XLS file every week to Google sheets format.

Thus go to specific Google drive folder. Find the latest or unconverted .XLS file. Convert to Google sheets format and save in the same folder.


回答1:


  • You want to convert one .xls file to Google Spreadsheet.
    • The .xls file has 60 kBytes in the file size.
    • The .xls file is put in the specific folder.
  • You want to put the converted Google Spreadsheet in the same folder with the .xls file.
  • You want a simple script for this situation.

If my understanding is correct, how about this sample script? In this sample script, the .xls file is converted by files.copy method of Drive API v2.

When you use this script, please enable Drive API at Advanced Google Services.

Sample script:

var folderId = "###"; // Please set the folder ID including the .xls file.

var files = DriveApp.getFolderById(folderId).getFilesByType(MimeType.MICROSOFT_EXCEL_LEGACY);
if (files.hasNext()) Drive.Files.copy({mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: folderId}]}, files.next().getId());

Note:

  • This sample script is for the .xls file. If you want to use .xlsx file, please modify from MimeType.MICROSOFT_EXCEL_LEGACY to MimeType.MICROSOFT_EXCEL.

References:

  • Advanced Google services
  • Files: copy



回答2:


Does something like this work for you?


  try {

    fileName = fileName || "microsoft-excel.xlsx";

    var excelFile = DriveApp.getFilesByName(fileName).next();
    var fileId = excelFile.getId();
    var folderId = Drive.Files.get(fileId).parents[0].id;  
    var blob = excelFile.getBlob();
    var resource = {
      title: excelFile.getName(),
      mimeType: MimeType.GOOGLE_SHEETS,
      parents: [{id: folderId}],
    };

    Drive.Files.insert(resource, blob);

  } catch (f) {
    Logger.log(f.toString());
  }

}

thanks to ctrlq.org/



来源:https://stackoverflow.com/questions/56211700/what-is-the-most-efficient-way-to-convert-a-xls-to-google-sheets-in-app-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!