问题
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
.xlsfile to Google Spreadsheet.- The
.xlsfile has 60 kBytes in the file size. - The
.xlsfile is put in the specific folder.
- The
- You want to put the converted Google Spreadsheet in the same folder with the
.xlsfile. - 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
.xlsfile. If you want to use.xlsxfile, please modify fromMimeType.MICROSOFT_EXCEL_LEGACYtoMimeType.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