- There are Excel files (filename with the extensions of .xlsx or .xls) in a folder with several subfolders.
- There are Spreadsheet files (filename without the extensions of .xlsx or .xls) in a folder without subfolders.
- You want to overwrite the existing Spreadsheet files with the converted Spreadsheet from Excel files.
- Number of Spreadsheet and Excel files are the same.
From your question and comments, I could understand like above.
At first, I tested to update files by the batch request. As the result, it seems that the update of file cannot be achieved by the batch request, when the file blob is used for updating. About this, if I found the workaround for this situation, I would like to update my answer.
So in this sample script, I propose the method for using Drive API of Advanced Google Services for above situation.
When you use this script, please enable Drive API at Advanced Google Services and API console. You can see about this at here.
Flow:
The flow of this script is as follows.
- Retrieve files in the source and destination folder.
- When the filenames in the source folder are existing in the destination folder, those files overwrite the existing Spreadsheet files.
- When the filenames in the source folder are not existing in the destination folder, those files are converted to Spreadsheet as new files.
Sample script:
Before run the script, please set sourceFolderId
and destinationFolderId
.
function myFunction() {
var sourceFolderId = "###"; // Folder ID including source files.
var destinationFolderId = "###"; // Folder ID that the converted files are put.
var getFileIds = function (folder, fileList, q) {
var files = folder.searchFiles(q);
while (files.hasNext()) {
var f = files.next();
fileList.push({id: f.getId(), fileName: f.getName().split(".")[0].trim()});
}
var folders = folder.getFolders();
while (folders.hasNext()) getFileIds(folders.next(), fileList, q);
return fileList;
};
var sourceFiles = getFileIds(DriveApp.getFolderById(sourceFolderId), [], "mimeType='" + MimeType.MICROSOFT_EXCEL + "' or mimeType='" + MimeType.MICROSOFT_EXCEL_LEGACY + "'");
var destinationFiles = getFileIds(DriveApp.getFolderById(destinationFolderId), [], "mimeType='" + MimeType.GOOGLE_SHEETS + "'");
var createFiles = sourceFiles.filter(function(e) {return destinationFiles.every(function(f) {return f.fileName !== e.fileName});});
var updateFiles = sourceFiles.reduce(function(ar, e) {
var dst = destinationFiles.filter(function(f) {return f.fileName === e.fileName});
if (dst.length > 0) {
e.to = dst[0].id;
ar.push(e);
}
return ar;
}, []);
if (createFiles.length > 0) createFiles.forEach(function(e) {Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: destinationFolderId}], title: e.fileName}, DriveApp.getFileById(e.id))});
if (updateFiles.length > 0) updateFiles.forEach(function(e) {Drive.Files.update({}, e.to, DriveApp.getFileById(e.id))});
}
Note:
- When you have many files for converting and the execution time of the script is over, please divide the files and run the script.
References:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…