Reputation: 85
I am struggling with code that does everything I want, but returns an error message at the end of the function, which prevents the rest of the script from running.
I am using this script:
function abc(){
var fileName = "file name";
var tabName = "tab";
clear(tabName,"A:AE");
var excelFile = DriveApp.getFilesByName(fileName).next();
var fileId = excelFile.getId();
var folderId = "folder id";
var blob = excelFile.getBlob();
var resource = {
title: excelFile.getName(),
mimeType: MimeType.GOOGLE_SHEETS,
parents: [{id: folderId}],
};
var id = Drive.Files.insert(resource,blob).id;
var newsheet = SpreadsheetApp.openById(id).getSheets()[0].getRange("A:AE").getValues();
SpreadsheetApp.getActive().getSheetByName(tabName).getRange("A:AE").setValues(newsheet);
Logger.log("ok so far");
Drive.Files.remove(id);
}
I want to delete the file I just created, which it does perfectly, but it generates an error message as follows:
Document 1b0pLioLpwZndFuW4kRQzxd5gkfZjWFIT5Qr8HV-LJvo is missing (perhaps it was deleted, or you don't have read access?)
It creates the file, copies the values to another spreadsheet, and deletes the file created, just as it has to be. And then the script ends, and I need to run other functions as well.
Any idea how to fix it?
I also tried DriveApp.removeFile(file),
but the script finishes and does not delete the file from the folder.
This is the error message:
"Document 1TVRiqJKN_bSXLW8B02XzUlTOUgT192prUYYwvBwu89w is missing (perhaps it was deleted, or you don't have read access?)"
and this is the logs the script generated:
[19-11-19 08:58:22:466 PST] TSC cleaned
[19-11-19 08:58:22:466 PST] TSC has started
[19-11-19 08:58:26:737 PST] TSC file generated on ID = 1TVRiqJKN_bSXLW8B02XzUlTOUgT192prUYYwvBwu89w
[19-11-19 08:58:27:363 PST] 1TVRiqJKN_bSXLW8B02XzUlTOUgT192prUYYwvBwu89w
[19-11-19 08:58:27:497 PST] TSC copied to destination sheet and id = 1TVRiqJKN_bSXLW8B02XzUlTOUgT192prUYYwvBwu89w
[19-11-19 08:58:28:171 PST] TSC file deleted
[19-11-19 08:58:30:323 PST] TSC imported
The last line "TSC Imported" is the last line of the function, so it looks like it is completing the function but in the end the message comes. I might have to try other options instead of the Drive API. Thank you for your help guys, I'll let you know if I succeed.
Upvotes: 2
Views: 1483
Reputation: 11
I was having this issue too and couldn't pin down exactly what was causing it but found that if you split up the file creation and deletion into separate functions and run them individually, you don't get this error. But, if you call both functions from within a third function, the error is thrown.
Runs fine when initiated manually...
createFile() {
let fileId = Drive.Files.insert(fileId, blob);
}
Also runs without error when initiated manually...
deleteFile() {
Drive.Files.remove(fileId);
}
Fails with error saying the file cannot be found...
createAndDelete() {
createFile();
deleteFile();
}
Upvotes: 1
Reputation: 11
I'm experiencing same problem. If I only run Drive.Files.insert
and next Drive.Files.remove
file is removed and no error message, but if I open file with SpreadsheetApp.openById
before Drive.Files.remove
then file is removed and script continue but I get that error. I was trying Utilities.sleep
with up to (barely sane) 20 seconds before remove and run script few times with random results(sometimes error sometimes without error).
How to fix that?
Upvotes: 1
Reputation: 85
Well guys I ended up creating an archive for the files so I won't delete them, I'll just leave them in this folder I created as an archive. Thank you for you help.
Upvotes: 0
Reputation: 2608
As @Alan Wells
mentioned in the comments, you are not giving Drive enough time to generate the file, copy the values and delete it.
Try adding Utilities.sleep(15000);
after the Drive.insert
.
More information here.
Upvotes: 0