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
Related
Can anyone help me with a script I am currently butchering to import file names into a sheet and add a checkbox to the appended row...
I am trying to get the file names into K7:K with a checkbox next to it in L7:L, Ive been playing around for hours and decided I may as well swallow my pride and ask a silly question on here!
My current code is:
function listFilesInFolder(folderName) {
SpreadsheetApp.getActive().toast("Populating Register...");
var sheetName = "Register"
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
//change the folder ID below to reflect your folder's ID
var folder = DriveApp.getFolderById("18KVczz-XW_3OSmCsVVIxd8p38maDzNm3");
var contents = folder.getFiles();
var cnt = 0;
var file;
while (contents.hasNext()) {
var file = contents.next();
cnt++;
data = [
file.getName(),
];
sheet.appendRow(data);
var checkbox = SpreadsheetApp.newDataValidation().requireCheckbox().build();
sheet.getRange("L:L").setDataValidation(checkbox).setValue("FALSE");
}
}
Obviously, its pretty far off, if anyone could help me out I'll be forever grateful! :)
Try this
function listFilesInFolder(folderName) {
SpreadsheetApp.getActive().toast("Populating Register...");
var sheetName = "Register"
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var folder = DriveApp.getFolderById("18KVczz-XW_3OSmCsVVIxd8p38maDzNm3");
var contents = folder.getFiles();
var cnt = 0;
var file;
while (contents.hasNext()) {
var file = contents.next();
cnt++;
sheet.getRange("K"+(6+cnt)).setValue(file.getName());
}
var checkbox = SpreadsheetApp.newDataValidation().requireCheckbox().build();
sheet.getRange("L7:L"+(6+cnt)).setDataValidation(checkbox).setValue("FALSE");
}
If you want to add new data :
function listFilesInFolder(folderName) {
SpreadsheetApp.getActive().toast("Populating Register...");
var sheetName = "Register"
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var folder = DriveApp.getFolderById("18KVczz-XW_3OSmCsVVIxd8p38maDzNm3");
var contents = folder.getFiles();
var cnt = sheet.getLastRow();
var file;
while (contents.hasNext()) {
var file = contents.next();
cnt++;
sheet.getRange("K"+(cnt)).setValue(file.getName());
}
var checkbox = SpreadsheetApp.newDataValidation().requireCheckbox().build();
sheet.getRange("L7:L"+(cnt)).setDataValidation(checkbox).setValue("FALSE");
}
I'm trying to run the following script and populate all rows which match the file name (test1.pdf) in google drive folder.
There are multiple rows which has the same file name, so I need to bring the same link to each row if it matches.
I tried the following code, but it does not work if there are blank rows in between the rows. Would appreciate your help on this. Thanks in advance.
Expected Result : Match the names in Column K and populate hyperlink in Column M in respective row, if "Yes" is not mentioned in COlumn N in the row. (There are blank rows in between these rows)
function myGDriveInt() {
var folderId = 'myfileid';
var folder = DriveApp.getFolderById(folderId)
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheetByName('Sheet1');
var RowSize=sheet.getRange('K:K').getValues().filter(String).length;
var file_name=sheet.getRange('K3:K'+RowSize).getValues()
var file_checker=sheet.getRange('N3:N'+RowSize).getValues()
for (i = 0; i < file_name.length; i++) {
if (file_checker[i][0]=="Yes"){continue;}
var files = folder.getFilesByName(file_name[i][0])
while (files.hasNext()) {
var childFile = files.next();
var file_url = childFile.getUrl()
sheet.getRange(i+3,13).setValue(file_url);
} // while
} //for
} // function
Try it this way:
function myGDriveInt() {
var folderId = 'myfileid';
var folder = DriveApp.getFolderById(folderId)
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getSheetByName('Sheet1');
var file_name=sh.getRange(3,11,sh.getLastRow()-2,1).getValues();//k
var col13=sh.getRange(3,13,sh.getLastRow()-2,1).getValues();//m
var file_checker=sh.getRange(3,14,sh.getLastRow()-2,1).getValues()//n
for (var i=0;i<file_name.length;i++) {
if(file_checker[i][0]!="Yes") {
var files=folder.getFilesByName(file_name[i][0]);
while (files.hasNext()) {
var childFile=files.next();
var file_url=childFile.getUrl();
col13[i][0]=file_url;//if you have more than one file with that name you will only get the last one
}
}
}
sh.getRange(3,13,col13.length,1).setValues(col13);//store results in m all at one time. Much quicker
}
So my question is, if I have a bunch of single sheet google sheets files in a folder, what is the best way script-wise to go about copying all of those individual file sheets to a master compilation spreadsheet with a sheet within that for every one of those files copied? I am writing it manually per file right now but I'm sure there is a quicker way using loops with like [0] [1]... for the different file names, but I don't know how to do that. Any help would be appreciated.
function ArrayBuilder() {
var filesource = DriveApp.searchFiles('title contains "X" and parents in "File_ID"');
while(filesource.hasNext()){
var File = filesource.next();
var ID = File.getId();
}
var name = File.getName()
var source = SpreadsheetApp.openById(ID);
var sheet = source.getSheets()[0];
var destinationID = "File_ID";
var destination = SpreadsheetApp.openById(destinationID);
sheet.copyTo(destination).setName(name);
var filesource = DriveApp.searchFiles('title contains "Y" and parents in "File_ID"');
while(filesource.hasNext()){
var File = filesource.next();
var ID = File.getId();
}
var name = File.getName()
var source = SpreadsheetApp.openById(ID);
var sheet = source.getSheets()[0];
var destinationID = "File_ID";
var destination = SpreadsheetApp.openById(destinationID);
sheet.copyTo(destination).setName(name);
}
You can use the following code:
var DESTINATION_SPREADSHEET_ID = '';
var PARENT_FOLDER_ID = '';
function buildMasterSpreadsheet() {
var destinationSpreadsheet = SpreadsheetApp.openById(DESTINATION_SPREADSHEET_ID);
var searchQuery = Utilities.formatString("'%s' in parents and mimeType = 'application/vnd.google-apps.spreadsheet'", PARENT_FOLDER_ID);
var sourceFiles = DriveApp.searchFiles(searchQuery);
while(sourceFiles.hasNext()){
var file = sourceFiles.next();
var sourceSheet = SpreadsheetApp.openById(file.getId()).getSheets()[0];
sourceSheet.copyTo(destinationSpreadsheet).setName(file.getName());
}
}
Of course, you will have to set the variables on top to the appropriate values. This script will:
Search files that are of type Sheets and that PARENT_FOLDER_ID are in their parents.
For each of the files found:
Copy its first sheet into the destination Spreadsheet.
You can also add any other clauses you may need into the search query.
I need a script to use with google sheets to save a list of .img urls to specific google drive folder with file naming from another cell.
For Example:
Column A - File URL
/imagefilepath.jpg
Column B - Save as name
image_1
Auto save /imagefilepath.jpg to google drive folder "Test" with "image_1" file name.
Is that possible?
New to scripts.. need some advice/pointers or working script (even better)!
Currently got the following:
/**
* Uploads a new file to the user's Drive.
*/
function uploadFile() {
var imgurl = 'https://www.w3schools.com/w3css/img_lights.jpg';
var image = UrlFetchApp.fetch(imgurl).getBlob();
var filename = setName('test');
var folder = DriveApp.getFoldersByName(folder);
if (folder != null) {
var file = DriveApp.createFile(image);
}
Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}
But it saves as url name into the root of google drive and only if I insert the img urls into the script. How do i make it dynamic?
P.s found a few scripts on here but all seem to be outdated/dont work. I have over 2000 urls and different file names.
Try this:
function uploadFile() {
var imgurl = 'https://www.w3schools.com/w3css/img_lights.jpg';
var image = UrlFetchApp.fetch(imgurl).getBlob().getAs('image/jpeg').setName('test');
var folder = DriveApp.getFolderById('FolderId');
var file = DriveApp.createFile(image);
Drive.Files.update({"parents": [{"id": folder.getId()}]}, file.getId());
}
So your final function should look something like this:
function uploadFiles() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var vA=rg.getValues();
for(var i=0;i<vA.length;i++) {
var imgurl=vA[i][0];//Column A for url
var name=vA[i][1];//Column B for filename
var image=UrlFetchApp.fetch(imgurl).getBlob().getAs('image/jpeg').setName(name);
var folder=DriveApp.getFolderById('1aIawMeJCjB1GWaV6URH3jkV4xUJhaYLV');
var file=DriveApp.createFile(image);
Drive.Files.update({"parents": [{"id": folder.getId()}]}, file.getId());
}
}
I made a modification to MetaMan's great answer so that the QRs (in column A1:A) for URLs to a Google Form with pre-fill responses could be uploaded to the Drive (with the new names for the files in column B1:B).
function uploadFiles() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('MY SHEET NAME');
var rg=sh.getDataRange();
var vA=rg.getValues();
for(var i=0;i<vA.length;i++) {
var imgurl="https://api.qrserver.com/v1/create-qr-code/?size=300x300&data="+encodeURIComponent(vA[i][0]);//Column A for url
var name=vA[i][1];//Column B for filename
var image=UrlFetchApp.fetch(imgurl).getBlob().getAs('image/jpeg').setName(name);
var folder=DriveApp.getFolderById('MY FOLDER ID');
var file=DriveApp.createFile(image);
Drive.Files.update({"parents": [{"id": folder.getId()}]}, file.getId());
}
}
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.