Google Script get files names into spreadsheet issues - google-apps-script

I made a code App Script that lists all the Folder Names and Files names into a Spreadsheet which works good until I rename one of the folders or file. Then the next time I run the script it will not include the renamed Folder/File into the Spreadsheet. Is there something I have done wrong in the script?
Thanks in advance for the help.
Link to spreadsheet: https://docs.google.com/spreadsheets/d/1T3eKmyXKhLCbcoMdZRzMQ1__-dL3bTlH0wMKaWVFJ1Y/edit?usp=sharing
Link to Google Script: https://script.google.com/d/1LVTe29Y3jDbElt8LM4ciZ8Kj0gPzmFAxawZaYmRaFvkkfrewtJ5fxO0B/edit?usp=sharing
Link to Folder: https://drive.google.com/drive/folders/1VPQC9Cp4QT0r_ILmq9c0nOsSSduqh6jg?usp=sharing
function listFolders(folder) {
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1T3eKmyXKhLCbcoMdZRzMQ1__-dL3bTlH0wMKaWVFJ1Y/edit#gid=0');
var first = ss.getSheetByName("Sheet1");
first.clear("A2:D405");
var sheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1T3eKmyXKhLCbcoMdZRzMQ1__-dL3bTlH0wMKaWVFJ1Y/edit#gid=0');
sheet.appendRow(["File Name", "URL", "Folder Name"]);
var folder = DriveApp.getFolderById('1VPQC9Cp4QT0r_ILmq9c0nOsSSduqh6jg');
var subfolders = folder.getFolders();
while (subfolders.hasNext()) {
//Logger.log(folder);
var name = subfolders.next();
while (subfolders.hasNext()) {
//Logger.log(folder);
var name = subfolders.next();
var files = name.getFilesByType('application/vnd.google-apps.spreadsheet');
var cnt = 0;
var file;
while (files.hasNext()) {
var file = files.next();
cnt++;
Logger.log(file);
Logger.log(cnt);
data = [
file.getName(),
file.getUrl(),
name, //folder name
];
sheet.appendRow(data);
};
}
}
}

Related

Insert all photos from one google drive folder into google Sheet?

I am not much of a coder, and only smart enough to modify code slightly. But I am hoping someone can help me. I am looking to upload all images in a google folder into a spread sheet with a script. I need the folder name to be changeable based on a cell, so I can change the name of the cell to a different folder run the script and the script will bring in all photos from the folder. Some folders might have 5 photos, others might have 25 photos. I have been able to figure it out but only if I reference the actual photo, and not just reference the name of the folder and have it import all. Any and all help is thanked in advance.
function insertImages() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const nameF = 'Folder'; // Sheet Name of the sheet containing the Folder Name
const rangeF = 'A1'; // Cell containing the Folder Name
const nameT = 'Target'; // Sheet Name of the sheet to insert images
let row = 2; // Starting row to insert
const col = 1; // Column to insert
const sheet = ss.getSheetByName(nameT);
const folderName = ss.getSheetByName(nameF).getRange(rangeF).getValue();
const folders = DriveApp.getFoldersByName(folderName);
while (folders.hasNext()) {
const folder = folders.next();
const files = folder.getFiles();
while (files.hasNext()) {
const file = files.next();
sheet.insertImage(file.getBlob(), col, row++);
}
}
}
by ID
function importImgs1() {
var folderID = '1ZfWEnxtKQiuz8V9j2ckgIZRHRMmYc7rH';
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('import images');
sh.clear();
sh.appendRow(["name", "image"]);
var folder = DriveApp.getFolderById(folderID);
var data = [];
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
data = [
file.getName(),
"=image(\"https://docs.google.com/uc?export=download&id=" + file.getId() +"\")",
];
sh.appendRow(data);
}
}
by Name
function importImgs2() {
var folderName = 'img';
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('import images by name');
sh.clear();
sh.appendRow(["name", "image"]);
var folders = DriveApp.getFoldersByName(folderName);
var foldersnext = folders.next();
var data = [];
var files = foldersnext.getFiles();
while (files.hasNext()) {
var file = files.next();
data = [
file.getName(),
"=image(\"https://docs.google.com/uc?export=download&id=" + file.getId() +"\")",
];
sh.appendRow(data);
}
}

Get folders and files listed in Google Sheets from a google drive folder using a Google Apps script

I have a Google sheet where a user can cut and paste the google drive folder URL, and it will extract the folder ID. This is extracted in a named range "FolderID".
I then want to list the folders and files in the folder (subfolders and files within as well if possible) into the worksheet "Files".
I have played around with another script I wrote, but am coming up with an error for the initial folders list (I haven't gotten to the files list yet).
I get this error: Exception: Unexpected error while getting the method or property getFolderById on object DriveApp.
The current script is:
function listFoldersAndFilesInAFolder() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('Files');
var foldername = ss.getRangeByName('FolderID');
sh.clearContents();
sh.appendRow(["Name", "Link"]);
var fA = [];
var folders = DriveApp.getFolderById(foldername).getFolders();//Enter folder id
while (folders.hasNext()) {
var folder = folders.next();
var folderURLstart = "https://drive.google.com/drive/folders/"
fA.push([folder.getName(), folderURLstart + folder.getId()]);
}
sh.getRange(sh.getLastRow() + 1, 1, fA.length, 2).setValues(fA);
sh.getRange(2, 1, sh.getLastRow() - 1, 2).sort({ column: 1, ascending: true });
}
Can anyone help:
fix the script above so I can list folders.
direct me on what to add to list files, and subfolders and files?
To add list of files, subfolders and files, use
function listFilesAndFolders() {
var folderid = '18akqHAN7PSPMnG3h5HpCskQsMCv4TqCM'; // change FolderID
var sh = SpreadsheetApp.getActiveSheet();
sh.clear();
sh.appendRow(["parent","folder", "name", "update", "size", "URL", "ID", "description", "type"]);
try {
var parentFolder =DriveApp.getFolderById(folderid);
listFiles(parentFolder,parentFolder.getName())
listSubFolders(parentFolder,parentFolder.getName());
} catch (e) {
Logger.log(e.toString());
}
}
function listSubFolders(parentFolder,parent) {
var childFolders = parentFolder.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
Logger.log("Fold : " + childFolder.getName());
listFiles(childFolder,parent)
listSubFolders(childFolder,parent + "|" + childFolder.getName());
}
}
function listFiles(fold,parent){
var sh = SpreadsheetApp.getActiveSheet();
var data = [];
var files = fold.getFiles();
while (files.hasNext()) {
var file = files.next();
data = [
parent,
fold.getName(),
file.getName(),
file.getLastUpdated(),
file.getSize(),
file.getUrl(),
file.getId(),
file.getDescription(),
file.getMimeType()
];
sh.appendRow(data);
}
}
https://docs.google.com/spreadsheets/d/13fHMrWR9OOWa045uJbkz67YoVkOG4Pg6ScvrRpE-WBc/copy

Issues with code for google-script (from drive to sheet)

I am currently looking to get file names from google drive to my google sheet. I have around 30 files in my drive, and I want their file name to appear in my google sheets. All files are located in one folder.
I have tried to do this through script google, but something seems to be wrong as I get an error code. Below is the code I found through other sites but something goes wrong....
function list_all_files_inside_one_folder_without_subfolders(){
var sh = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById('1HPv9-umg0XQ8Fa9UV8lDr6O2Y4kAIAJe'); // I change the folder ID here
var list = [];
list.push(['Name','ID','Size']);
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
var row = []
row.push(file.getName(),file.getId(),file.getSize())
list.push(row);
}
sh.getRange(1,1,list.length,list[0].length).setValues(list);
}
Try this:
function list_all_files_inside_one_folder_without_subfolders(){
var sh = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById('1HPv9-umg0XQ8Fa9UV8lDr6O2Y4kAIAJe');
var list = [];
list.push(['Name','ID','Size']);
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
list.push([file.getName(),file.getId(),file.getSize()]);
}
sh.getRange(1,1,list.length,list[0].length).setValues(list);
}

Get all files in Folder and Subfolders Gscript

I have a gscript that will return all the files in a folder adn write to the open spreadsheet. I would like to add the files in all the subfolders as well but can't quite make it work. Here is the script for the files in the folder with the contents of the subfolders:
function getFiles() {
// Get the active spreadsheet file and the active sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssid = ss.getId();
// Look in the current folder e.g. if this spreadsheet is in 'My Folder'
// this routine will return all of the files in 'My Folder'.
var ssparents = DriveApp.getFileById(ssid).getParents();
var sheet = ss.getActiveSheet();
// Clear the area, add the headers ready for results
var headers = [["Full Folder Ref", "File Name", "Last Updated", "File Owner", "File URL", "In Folder"]];
sheet.getRange("A1:F").clear();
sheet.getRange("A1:F1").setValues(headers);
// Loop through all the files, add the values to the spreadsheet.
var folder = ssparents.next();
var files = folder.getFiles();
var i=1;
while(files.hasNext()) {
var file = files.next();
// get the full folder ref
var fileParents = file.getParents()
folders = [];
while (fileParents.hasNext()) {
fileParents = fileParents.next();
folders.push(fileParents.getName());
fileParents = fileParents.getParents();
}
// create the row
if(ss.getId() == file.getId()){
continue;
}
sheet.getRange(i+1, 1, 1, 6).setValues([[folders.reverse().join("/") , file.getName(), file.getLastUpdated(),file.getOwner().getName(), file.getUrl(), folder.getName() ]]);
i++;
}
}
Can someone help me add the subfolder files please.
thank you

Google Script Separate Folders and Subfolders

I'm a beginner coder and am using code I found on the internet to create a list of all folders and subfolders from a Google Drive folder. I'm able to generate that list, but I have two issues:
How do I increase the run speed? I keep hitting the max of 6 minutes for executing a function.
How can I tell the difference between a folder and subfolder? I can run the code and see the list of folders, but I would like a way to differentiate between what is a primary folder and what are the secondary folders within that folder before looking at a new one.
I appreciate any help I can get.
Heres the code I'm using
function listFolderContents() {
var foldername = 'name of folder';
var folderlisting = 'listing of folder ' + foldername;
var ss = SpreadsheetApp.create(folderlisting);
var sheet = ss.getActiveSheet();
var folders = DriveApp.getFoldersByName(foldername)
var folder = folders.next();
//listFilesInFolder(folder, sheet);
traverseFolder(folder, sheet);
};
function traverseFolder(folder, sheet) {
listFilesInFolder(folder, sheet);
var subFolders = folder.getFolders();
while (subFolders.hasNext()) {
traverseFolder(subFolders.next(), sheet);
}
}
function listFilesInFolder(folder, sheet) {
var foldername = folder.getName();
var contents = folder.getFiles();
sheet.appendRow( [foldername] );
while(contents.hasNext()) {
var file = contents.next();
}
}