Convert XLS files to Google Spreadsheet using Google Apps Script
My name is Alexander and I am a freelancer, my main specialization is Google Apps Script. One of the customers needed to programmatically convert Microsoft Excel files to Google Spreadsheets. I had never encountered such a task before, so I called Google for help. I rummaged through a bunch of forums, but did not find a ready-made solution. I had to write my bike. And although the code turned out to be short, it took quite a while to find a solution. Here is what happened:
This function takes as parameter the ID of the folder in which the files that need to be converted are located. Finds files corresponding to the MimeType.MICROSOFT_EXCEL_LEGACY type in this folder, these are Microsoft Excel XLS documents, and creates copies of them in the same folder. When copying, the format is converted, the convert: true parameter is responsible for this. If the name .xls is present in the original file name, then it is deleted.
If you need to process XLSX files, then change the type to MimeType.MICROSOFT_EXCEL and
But if you just copy this code and try to run, it will throw an error '' ReferenceError: The “Drive” object is not defined. "To fix it, you need to select the item“ Additional Google functions ... ”in the“ Resources ”menu in the“ Resources ”menu . In the window that appears, find the "Drive API" and turn it on.

Then, in the "Resources" menu, select the "Project Developers Console ..." item. In the window that appears, click on the link with the project ID.

On the page that appears, in the left menu choose «APIs & auth» and then «APIs» A list of API, there go on «Drive API» link..

Push the button «Enable API» -. You can cook run.
Similarly, you can convert files of other formats.
I hope that this article will save someone time.
function convert(folderId) {
var folderIncoming = DriveApp.getFolderById(folderId);
var files = folderIncoming.getFilesByType(MimeType.MICROSOFT_EXCEL_LEGACY);
while (files.hasNext()) {
var source = files.next();
var sourceId = source.getId();
var fileName = source.getName().replace('.xls', '');
var file = {
title: fileName,
};
file = Drive.Files.copy(file, sourceId, {convert: true});
}
}
This function takes as parameter the ID of the folder in which the files that need to be converted are located. Finds files corresponding to the MimeType.MICROSOFT_EXCEL_LEGACY type in this folder, these are Microsoft Excel XLS documents, and creates copies of them in the same folder. When copying, the format is converted, the convert: true parameter is responsible for this. If the name .xls is present in the original file name, then it is deleted.
If you need to process XLSX files, then change the type to MimeType.MICROSOFT_EXCEL and
var fileName = source.getName().replace('.xls', '');
на
var fileName = source.getName().replace('.xlsx', '');
But if you just copy this code and try to run, it will throw an error '' ReferenceError: The “Drive” object is not defined. "To fix it, you need to select the item“ Additional Google functions ... ”in the“ Resources ”menu in the“ Resources ”menu . In the window that appears, find the "Drive API" and turn it on.

Then, in the "Resources" menu, select the "Project Developers Console ..." item. In the window that appears, click on the link with the project ID.

On the page that appears, in the left menu choose «APIs & auth» and then «APIs» A list of API, there go on «Drive API» link..

Push the button «Enable API» -. You can cook run.
Similarly, you can convert files of other formats.
I hope that this article will save someone time.