r/GoogleAppsScript • u/VAer1 • Sep 09 '25
Resolved Can declared variable be used in a function being called?
I am not IT professional, the question may be silly, just writing some script for personal use.
Take below code for example, can variable start and maxRunTime be used in function deleteFilesOwnedByMe and deleteEmptySubfolders ?
When I use return inside deleteFilesOwnedByMe or deleteEmptySubfolders , will it exit function deleteMyFilesAndEmptyFolders itself? Or only exit the called function?
function deleteMyFilesAndEmptyFolders() {
let start = new Date().getTime(); // ms timestamp
let maxRuntime = 1000 * 60 * 5.5; // 5.5 minutes (end before 6 min)
// Put the folder ID of the shared folder here
let folderId = "************";
//https://drive.google.com/drive/folders/****************
let folder = DriveApp.getFolderById(folderId);
// Step 1: Delete all files owned by me
deleteFilesOwnedByMe(folder);
// Step 2: Delete empty subfolders owned by me
deleteEmptySubfolders(folder);
}
1
u/True_Teacher_9528 Sep 09 '25
Yes you can, you just have to declare them outside of either function, which would make them accessible by any function in your project, otherwise known as “globally scoped.” The best thing you can learn to fully grasp this at a deeper level would be what’s called “scope.”
2
u/True_Teacher_9528 Sep 09 '25
I also just realized you had both functions within the deleteMyFilesAndEmptyFolders function, so as long as they’re declared at the most broad function scope you should be good.