Insert all photos from one google drive folder into google Sheet? - google-apps-script

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

Related

Trying to copy the data from a sheet inside a folder in Google Drive to a different sheet but the original changes every day with different id's

Every day there is a new report that I want to copy to a master sheet outside of the folder, I'm using a different script to transfer the file from my Gmail to a folder in Drive, there is only one file at a time in the folder. I tried getting the id of the file with this code and then use the id to copy and paste the info. I'm still trying to understand how google scripts works so I know the code is not functional, I would like to know if what I want to do is possible and a guide to what I could do.
function obtainId() {
var folder = DriveApp.getFolderById('ID');
var files = folder.getFiles();
var file = files.next();
var id = DriveApp.getFileById(file.getId());
var ss = SpreadsheetApp.getActiveSpreadsheet(id);
var copySheet = ss.getSheetByName("name");
var pasteSheet = ss.getSheetByName("name");
var source = copySheet.getRange(range);
var destination = pasteSheet.getRange(range);
source.copyTo(destination);
}
Obtain Id copy data for a file with today's date in particular folder
function obtainId() {
let dt = new Date();
let tdv = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()).valueOf();
var folder = DriveApp.getFolderById('ID');
var files = folder.getFilesByType(mimeType.GOOGlE_SHEETS);
let id;
while (files.hasNext()) {
let file = files.next(;
if (file.getDateCreated().valueOf() > tdv) {
id = file.getId();
break;
}
}
const ss = SpreadsheetApp.openById(id);
const csh = ss.getSheetByName("copy sheet name");
const psh = ss.getSheetByName("paste sheet name");
csh.getRange().copyTo(psh.getRange();)
}
Using Sheets API:
function obtainId() {
let dt = new Date();
let tdv = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()).valueOf();
var folder = DriveApp.getFolderById(gobj.globals.folder3id);
var files = folder.getFilesByType(MimeType.GOOGLE_SHEETS);
let id;
while (files.hasNext()) {
let file = files.next();
if (file.getDateCreated().valueOf() > tdv) {
id = file.getId();
break;
}
}
const ss = SpreadsheetApp.openById(id);
const csh = ss.getSheetByName("Sheet1");
const dss = SpreadsheetApp.getActive();
const psh = dss.getSheetByName("Sheet2");
let crg = csh.getDataRange().getA1Notation();
let vs = Sheets.Spreadsheets.Values.get(ss.getId(),`${csh.getName()}!${crg}`).values;
prg = psh.getRange(psh.getLastRow() + 1,1,vs.length,vs[0].length).getA1Notation();
Sheets.Spreadsheets.Values.update({values: vs}, dss.getId(), psh.getSheetName(), {valueInputOption: "USER_ENTERED"});
}

Google Sheets AppScript Export Cell to HTML file with dynamic folder name

I currently have an app script that exports the contents of a cell into a HTML file in google sheets below:
function saveToTextfile() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var rg = sh.getRange(1, 1, sh.getLastRow(), 2);
var vs = rg.getValues();
var folder = DriveApp.getFolderById('1rTOhLtYzFAtPH-NuO-uu7OXYH4Ri6vFK')
var files = folder.getFiles();
while (files.hasNext()) files.next().setTrashed(true);
vs.forEach(r => folder.createFile(r[0] + '.html', r[1]) );
}
This works perfectly, however, I was wanting to add in another step to export the file to a folder name which would also be based on a cell value. So for example, instead of simply exporting the files "Index-1.html", "Index-2.html" etc, I want to export this way "/1/index.html", "/2/index.html". I hope that is clear.
Any help would be very much appreciated :)
Try
function saveToTextfile() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var rg = sh.getRange(1, 1, sh.getLastRow(), 2);
var vs = rg.getValues();
vs.forEach(r => DriveApp.getFolderById(getChildId('1rTOhLtYzFAtPH-NuO-uu7OXYH4Ri6vFK', r[0])).createFile('index.html', r[1]));
}
function getChildId(parentId, name) {
var parent = DriveApp.getFolderById(parentId);
var id;
var folders = parent.getFoldersByName(name);
if (folders.hasNext()) {
var folder = folders.next();
id = folder.getId();
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
if (file.getName() == 'index.html') { file.setTrashed(true) }
}
}
else {
var folder = parent.createFolder(name);
id = folder.getId();
}
return id;
}

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

Can I create a list of images in my Google Drive (including file name and share link)?

I have folders with several hundred images on Google Drive. I want to generate a spreadsheet by folder of the file name and also it's shareable link so that I can create a CSV file for bulk linking elsewhere.
I can bulk copy the shareable links, not with code, but by selecting all files in Google Drive, right clicking and using the share function. This produces a list of links that can be copy & pasted to a spreadsheet. However, I can't match them with file names.
I have found a script (below) to export a list of names and direct links (not share links), but can't get the order of either export to match up.
The script used to list file names and direct links was as follows:
// replace your-folder below with the folder for which you want a listing
function listFolderContents() {
var foldername = 'your-folder';
var folderlisting = 'listing of folder ' + foldername;
var folders = DriveApp.getFoldersByName(foldername)
var folder = folders.next();
var contents = folder.getFiles();
var ss = SpreadsheetApp.create(folderlisting);
var sheet = ss.getActiveSheet();
sheet.appendRow( ['name', 'link'] );
var file;
var name;
var link;
var row;
while(contents.hasNext()) {
file = contents.next();
name = file.getName();
link = file.getUrl();
sheet.appendRow( [name, link] );
}
};
// replace your-folder below with the folder for which you want a listing
function listFolderContents() {
var foldername = 'your-folder';
var folderlisting = 'listing of folder ' + foldername;
var folders = DriveApp.getFoldersByName(foldername)
var folder = folders.next();
var contents = folder.getFiles();
var ss = SpreadsheetApp.create(folderlisting);
var sheet = ss.getActiveSheet();
sheet.appendRow(['name', 'link']);
var file;
var name;
var link;
var row;
while (contents.hasNext()) {
file = contents.next();
name = file.getName();
link = "https://drive.google.com/uc?export=download&id=" + file.getId();
sheet.appendRow([name, link]);
}
};

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