Reputation: 321
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.
Upvotes: 2
Views: 2630
Reputation: 1
I thought this would help you to convert for single/specific file by ID from MS Excel file to GS and return the new sheet Id converted to Google sheet.
function testConvertExcelToGS(fileId = '*your file id*') {
var file = DriveApp.getFileById(fileId);
Logger.log('file id %s',file.getName());
let blob = file.getBlob();
Logger.log('bolb file: %s:',blob);
let config = {
title: file.getName(),
parents: [{id:'*The folder id you want to copy/create GS version of MS Excel*'}],
mimeType: MimeType.GOOGLE_SHEETS,
convert: true
};
Logger.log('config title: %s', config.title);
let spreadsheet = Drive.Files.insert(config, blob);
Logger.log('The new spread sheet id: %s',spreadsheet.id);
return spreadsheet.id;
}
Upvotes: 0
Reputation: 201683
.xls
file to Google Spreadsheet.
.xls
file has 60 kBytes in the file size..xls
file is put in the specific folder..xls
file.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.
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());
.xls
file. If you want to use .xlsx
file, please modify from MimeType.MICROSOFT_EXCEL_LEGACY
to MimeType.MICROSOFT_EXCEL
.Upvotes: 1
Reputation: 69
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/
Upvotes: 1