I have a spreadsheet where column 1 contains source file IDs, with each cell holding only one ID. Column 2 has destination file IDs, where cells contain multiple IDs separated by commas. I utilize a script to retrieve these values and perform various operations. Below is an excerpt detailing the manipulations performed on the variables:
// Retrieve source and destination IDs from the spreadsheet
var toFileIds = myFile.getRange(2,2,key.getLastRow()-1, 1).getValues(); // Fetch destination file IDs from myFile
var sourceFileIds = myFile.getRange(2,1,key.getLastRow()-1, 1).getValues(); // Fetch source file IDs from myFile
// Write out the global variables
var stringToFileIds = JSON.stringify(toFileIds); // Convert to string as needed by PropertiesService
var stringSourceFileIds = JSON.stringify(sourceFileIds); // Convert to string as needed by PropertiesService
PropertiesService.getScriptProperties().setProperty('toFileIds', stringToFileIds);
PropertiesService.getScriptProperties().setProperty('sourceFileIds', stringSourceFileIds);
I also have a second script that examines a source file and identifies the corresponding destination files previously mentioned:
// Read in global variables
toFileIds = PropertiesService.getScriptProperties().getProperty('toFileIds');
sourceFileIds = PropertiesService.getScriptProperties().getProperty('sourceFileIds');
toFileIds = JSON.parse(toFileIds);
sourceFileIds = JSON.parse(sourceFileIds);
// Obtain the destination file IDs
var ArrPos = sourceFileIds.indexOf('activeFile'); // Locate the position of the active file name within the stored array
var destFileIds = destFileIds[ArrPos]; // Utilize this position to find the destination files
var destFileIds = destFileIds.toString().split(","); // Separate each comma-separated ID into individual elements in the array
// Determine if there are any destination files missing
if (destFileIds == undefined) {
var destFileIds = "no";
}
// Cache the variable
cache = CacheService.getPublicCache();
cache.put("destFileIds", JSON.stringify(destFileIds), 400);
Lastly, there's a third script containing an error checking snippet. It's in this segment that the problem arises:
if (destFileIds != "no") {
for (var i = 0; i <= destFileIds.length -1; i++) {
try {
DriveApp.getFileById(destFileIds[i]);
} catch (e) {
//Logger.log(e.message);
throw 'At least one of the destination files does not exist.';
}
}
}
In the final code excerpt, I anticipate an error notification only if an ID in the array does not exist. While this snippet works properly for the initial element when all elements of destFileIds
are present, it throws the defined error message for the second element in the array and stops thereafter. I've verified that all file IDs are correct manually by inserting them into the URL. Additionally, I manually created an array with these IDs which executed without errors:
var destFileIds = ["myID1", "myID2", "myID3"]; // Replace these with actual IDs
if (destFileIds != "no") {
for (var i = 0; i <= destFileIds.length -1; i++) {
try {
DriveApp.getFileById(destFileIds[i]);
} catch (e) {
//Logger.log(e.message);
throw 'No updates were made because all of the related (copy to) templates do not appear to exist.';
}
}
}
Any insights on why the second element may be treated differently than the first element of destFileIds
?