I am trying to run a small script that will pop up a small box and the user enters the drive folder ID and then the script will take that folderid and then list all the files inside that drive into the google sheet. These codes work when separated but when I try to put it together in one script. Its not working. The UI part is working but when entering a valid folder ID, I get this error:"ReferenceError: folderId is not defined" I have declared the folderID at least to my knowledge. Anyone can help me out regarding this matter?
function onOpen() {
var SS = SpreadsheetApp.getActiveSpreadsheet();
var ui = SpreadsheetApp.getUi();
ui.createMenu('List Files/Folders')
.addItem('List All Files and Folders', 'listFilesAndFolders')
.addToUi();
};
function listFilesAndFolders(){
var folderId = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
if (folderId === "") {
Browser.msgBox('Folder ID is invalid');
return;
}
list_all_files_inside_one_folder_without_subfolders(folderId, true);
};
function list_all_files_inside_one_folder_without_subfolders(){
var sh = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById(folderId);
var list = [];
list.push(['Name','ID','URL','Date Created']);
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
var row = []
row.push(file.getName(),file.getId(),file.getUrl(),file.getDateCreated()),
list.push(row);
}
sh.getRange(1,1,list.length,list[0].length).setValues(list);
}
CodePudding user response:
I cannot reproduce your error with this similar code:
function listFilesAndFolders(){
var folderId = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
if (folderId === "") {
Browser.msgBox('Folder ID is invalid');
return;
}
let name = DriveApp.getFolderById(folderId)
Logger.log(name)
};
CodePudding user response:
If your showing script is the script that is used with your actual situation, how about the following modification?
Modification points:
- In your script, you are calling
list_all_files_inside_one_folder_without_subfolders(folderId, true);
with 2 arguments. But, your function offunction list_all_files_inside_one_folder_without_subfolders(){,,,}
has no arguments. I thought that this might be the reason for your issue ofReferenceError: folderId is not defined
. - And, in your script, when the invalid folder ID is used, an error occurs.
When these points are reflected in your showing script, how about the following modification?
Modified script:
function listFilesAndFolders() {
var folderId = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
if (folderId === "") {
Browser.msgBox('Folder ID is invalid');
return;
}
try {
DriveApp.getFolderById(folderId);
} catch (e) {
Browser.msgBox('Folder ID is invalid');
}
list_all_files_inside_one_folder_without_subfolders(folderId, true);
};
function list_all_files_inside_one_folder_without_subfolders(folderId) {
var sh = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById(folderId);
var list = [];
list.push(['Name', 'ID', 'URL', 'Date Created']);
var files = folder.getFiles();
while (files.hasNext()) {
file = files.next();
var row = []
row.push(file.getName(), file.getId(), file.getUrl(), file.getDateCreated()),
list.push(row);
}
sh.getRange(1, 1, list.length, list[0].length).setValues(list);
}
In your script, it seems that 2nd argument of the function of
list_all_files_inside_one_folder_without_subfolders
is not used. If you want to use it, please modify the function.When this script is run, a dialog is opened. When the valid folder ID is inputted and ok is clicked, the folder ID is used in the function of
list_all_files_inside_one_folder_without_subfolders
. And, the file list is put on the active sheet. If the invalid folder ID is inputted, "Folder ID is invalid" is returned.