Reputation: 4883
I wrote a little script to convert all the .xls
and .xlsx
files in my google drive to google sheets. However, the results of the script are very unreliable, sometimes I get an error, in some random part of the code, saying: "We're sorry, a server error occurred. Please wait a bit and try again.". And other times I get an error saying "Access denied Drive App" on the line where I try to delete my converted .xls
or .xlsx
files. I have enabled the Advance Google Services for the Drive API.
function convertFolder(folder){
Logger.log('Folder name '+folder.getName());
var files = folder.searchFiles('(title contains ".xlsx" or title contains ".xls") and (not title contains ".~lock")');
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName().replace(/\.[^/.]+$/, "");
Logger.log('File name '+name);
if (name.indexOf(".~lock")!=-1)
continue; //for some reason the "and (not title contains ".~lock")" didn't do the trick - why!?!
var parents = xFile.getParents();
var parents_arr = new Array();
while (parents.hasNext()){
parents_arr.push({'kind':"drive#fileLink",'id':parents.next().getId()});
}
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = { title : name+'.gsheet',key : ID, parents: parents_arr, mimeType: "application/vnd.google-apps.spreadsheet"};
file = Drive.Files.insert(newFile, xBlob, {convert: true});
xFile.setTrashed(true);
}
var folders = folder.getFolders();
while (folders.hasNext())
convertFolder(folders.next());
}
function convertAllExcel(){
var folders = DriveApp.getFoldersByName('Data');
while (folders.hasNext()){
var folder = folders.next();
convertFolder(folder);
}
SpreadsheetApp.getUi().alert("done");
}
Upvotes: 2
Views: 2715
Reputation: 31
I had a similar problem! While running the script logged in as the main user seems to not have the maximum throughput limit, sharing a document with other users and letting them run the script that does writing operations in other user's drive does. 'Utilities.sleep(1500);' did the trick for me.
delFolder = DriveApp.getFolderById("folder_id")
delFiles = delFolder.getFiles();
while (delFiles.hasNext()) {
var delFile = delFiles.next();
Utilities.sleep(1500);
delFile.setTrashed(true);
Upvotes: 3
Reputation: 22306
I suspect you're hitting a rate limit. If you are doing more than 20 write operations (create, update, delete) in rapid succession, you need to throttle the requests to approximately one every 1.5 seconds.
You're probably thinking "wtf", but that's seriously the maximum throughput you can achieve with Drive.
Upvotes: 3