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

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);
}

Related

Check a specific file in Google drive folder with Google Script

I'm looking for a script to achieve the following. Tried googling few codes but they don't seem to address what I require. And since I'm new to scripts, I'm kind of lost on this.
Check name listed in "Column H" in google sheet and find matching file in Google Drive (need to give a path).
If matches, display the hyperlink to the file in "Column I"
And also the script should check through all the rows in the sheet, and do the above for each row.
And stop checking if Column "Column K" has as "Complete"
Thank you in advance.
The code I was trying the modify for this is follows;
var searchFor ='title contains "2013"';
var names =[];
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
var file = files.next();
names.push(file.getName());
}
var folders = DriveApp.searchFolders(searchFor);
while (folders.hasNext()) {
var file = folders.next();
names.push(file.getName());
}
for (var i=0;i<names.length;i++){
Logger.log(names[i]);
}
}```
Here is my solution. Feel free to change the size range of column H ('H2:Hx') :
function myFunction() {
var folderId = '#FolderId';
var folder = DriveApp.getFolderById(folderId)
var extension = '.xlsx';
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheetByName('Sheet1');
var RowSize=sheet.getRange('H:H').getValues().filter(String).length; //calculate max value of rows
var file_name=sheet.getRange('H2:H'+RowSize).getValues()
var file_checker=sheet.getRange('K2:K'+RowSize).getValues()
for (i = 0; i < file_name.length; i++) {
if (file_checker[i][0]=="Complete"){break;}
var files = folder.getFilesByName(file_name[i][0]+extension)
while (files.hasNext()) {
var childFile = files.next();
var file_url = childFile.getUrl()
sheet.getRange(i+2,9).setValue(file_url);
} // while
} //for
} // function

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();
}
}

Google Script get files names into spreadsheet issues

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);
};
}
}
}

Script to run on specific sheet

I have a script that I have been using, but it only works with, as written, the active sheet (pulls a list of documents from the specified directory in Google Drive). How can transform this to run on a specific sheet. The name of the sheet is "Per 7".
function list_all_files_inside_one_folder_without_subfolders(){
var sh = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById('0B4zzmqQYDRm2flZPRFVWd1FfSGpJTXFmcWlSLXVVTUZJRjNlU3QzTER6aHFsYVEzTDdrS00'); // I change the folder ID here
var list = [];
list.push(['Name']);
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
var row = []
row.push(file.getName())
list.push(row);
}
sh.getRange(1,1,list.length,list[0].length).setValues(list);
}
try changing:
var sh = SpreadsheetApp.getActiveSheet();
to
var sh = SpreadsheetApp.getActive().getSheetByName('Per 7');

A script to copy one sheet to various spreadsheets in a specific folder in Google Drive

I have a sheet which I need to copy to hundreds of spreadsheets in a given folder in Google Drive. I am just beginning with script and I do not know how to proceed because the script below just gives me an error. Thanks.
function myFunction() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[0];
var folder = DriveApp.getFoldersByName('TEST FOR SCRIPT');
var contents = folder.getFiles();
for (var i = 0; i < contents.length; i++) {
file = contents[i];
}
sheet.copyTo(contents).setName('ANSWERS');
}
I was given this answer by someone within a community in Google+ and it works:
function copyToSheets() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[1];
var folders = DriveApp.getFoldersByName('ID');
var firstFolder = folders.next();
var folderFiles = firstFolder.getFiles();
var thisFile;
while (folderFiles.hasNext()) {
thisFile = folderFiles.next();
var currentSS = SpreadsheetApp.openById(thisFile.getId());
sheet.copyTo(currentSS);
currentSS.getSheets()[currentSS.getSheets().length-1].setName('NAME');
}
}
This line of code:
var folder = DriveApp.getFoldersByName('TEST FOR SCRIPT');
returns a FolderIterator. Even if you only have one folder by that name, the getFoldersByName() method returns an object, that you can't use the getFiles() method on directly. The getFiles() method works on the Files Class. It is not available to the Folder Iterator. After you get the Folder Iterator, you must use the next() method to get the first (and probably only) folder in the object.
function getAllFilesInFolder() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[0];
var folders = DriveApp.getFoldersByName('Folder Name Here');
var firstFolder = folders.next();
var folderFiles = firstFolder.getFiles();
var thisFile;
while (folderFiles.hasNext()) {
thisFile = folderFiles.next();
Logger.log(thisFile.getName());
};
};
And also use hasNext() to loop through the files. There doesn't seem to be an length property of the Files object.