I'm trying to copy over an XLSX sheet to an existing Gsheet, both are in the same GDrive folder. This script works when I run it as Drive.Files.insert()
but that causes multiple version of the file to be created each time the script is run - so I switched it over to Drive.Files.update()
like below....
function importXLS(){
var myFolder= "XXXXXXXXXX"; // Set the folder ID of "FolderB" if you want to copy over to a different folder
var files = DriveApp.getFolderById(myFolder).searchFiles('title = "XXXXX.xlsx"');
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName();
if (name.indexOf('.xlsx')>-1){
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = {
title : name '_converted',
parents: [{'id': myFolder}],
supportsAllDrives: true
};
file = Drive.Files.update(newFile, xBlob, { //Originally using Drive.Files.insert() but was creating multiple versions. I want to overwrite the existing version.
convert: true, supportsAllDrives: true
});
}
}
}
But I'm getting the following error when this runs...
Exception: The mediaData parameter only supports Blob types for upload.
Any idea on what I'm doing wrong? This is my first time using Google Apps Script and javascript so I'm kinda flying blind here.
CodePudding user response:
I believe your goal is as follows.
- You have a folder in Google Drive. There are XLSX files and Google Spreadsheets in the folder.
- You want to overwrite the Google Spreadsheet with the XLSX file by searching the filename.
- Filename of the XLSX file is like
XXXXX.xlsx
. - Filename of the Google Spreadsheet file is like
XXXXX.xlsx_converted
.
- Filename of the XLSX file is like
Modification points:
- In the case of
Drive.Files.update
, it is required to include the file ID of the target file. - In your script, the file ID of the target Spreadsheet is not retrieved.
- The 2nd argument of
Drive.Files.update
is required to be the file ID. But, your script usesblob
. I thought that this might be the reason for your current issue.
- The 2nd argument of
When these points are reflected in your script, how about the following modification?
Modified script:
function importXLS() {
var myFolder = "XXXXXXXXXX"; // Please set the folder ID.
var filename = "XXXXX.xlsx"; // Please set the filename of XLSX file.
var folder = DriveApp.getFolderById(myFolder);
var files = folder.searchFiles(`title="${filename}" and mimeType="${MimeType.MICROSOFT_EXCEL}" and trashed=false`);
while (files.hasNext()) {
var xFile = files.next();
var name = xFile.getName();
if (name.indexOf('.xlsx') > -1) {
var xBlob = xFile.getBlob();
var spreadsheetFileName = name '_converted';
var spreadsheet = folder.searchFiles(`title="${spreadsheetFileName}" and mimeType="${MimeType.GOOGLE_SHEETS}" and trashed=false`);
if (spreadsheet.hasNext()) {
Drive.Files.update(null, spreadsheet.next().getId(), xBlob, { convert: true, supportsAllDrives: true });
} else {
var newFile = { title: spreadsheetFileName, parents: [{ 'id': myFolder }] };
file = Drive.Files.insert(newFile, xBlob, { convert: true, supportsAllDrives: true });
}
}
}
}
- When this script is run, the XLSX file of
XXXXX.xlsx
is retrieved from the specific folder. And, Google Spreadsheet of the filename ofXXXXX.xlsx_converted
is searched from the same folder. When the Spreadsheet is found, the Spreadsheet is overwritten by the XLSX file. When the Spreadsheet is not found, a new Spreadsheet is created by converting the XLSX file.