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');
Related
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);
}
}
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);
}
I am trying to change a value of a Google sheet cell in multiple google sheets using Google Script.
I have a Folder that has 80 subfolders one for each agent. Within each subfolder I have a Google Sheet with a title of "Time Management Agent View - nameOfAgent". I want to set the value of Cell B4 in each sheet [1] to a new value.
I have tried this and it does not work. When I try to log the agentSheet, It comes out blankAs is probably evident from the code i am very new and untrained in this ;) Thank you for your help.
Filip
var regex = "'Time Management Agent View' + /.*/"
var agentsFolderId = 'Folder_ID';
var folders = DriveApp.getFolderById(agentsFolderId).getFolders();
while (folders.hasNext()) {
var agentFolder = folders.next();
var allAgentsSheets = agentFolder.getFilesByName(regex);
Logger.log(agentFolder);
Logger.log(allAgentsSheets);
while (allAgentsSheets.hasNext()){
var agentSheet = allAgentsSheets.next();
Logger.log(agentSheet);
SpreadsheetApp.setActiveSpreadsheet(agentSheet);
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
SpreadsheetApp.setActiveSheet(activeSpreadsheet.getSheets()[1]);
var activeSheet = SpreadsheetApp.getActiveSheet()
SpreadsheetApp.setActiveRange(activeSheet.getRange(2, 4))
var cellToChange = SpreadsheetApp.getActiveRange()
cellToChange.setValue('Test')
}}
}
I figure it out!
var str = 'Time Management Agent View'
var regex = /.*/
var agentsFolderId = 'Folder_ID';
var folders = DriveApp.getFolderById(agentsFolderId).getFolders();
while (folders.hasNext()) {
var agentFolder = folders.next();
var allAgentsSheets = agentFolder.searchFiles('title contains "Time Management Agent View"');
Logger.log(agentFolder);
Logger.log(allAgentsSheets);
while (allAgentsSheets.hasNext()){
var agentSheet = allAgentsSheets.next().getId();
SpreadsheetApp.setActiveSpreadsheet(SpreadsheetApp.openById(agentSheet));
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1];
var cell = sheet.getRange("B4");
cell.setFormula('New_Formula');
var cellContent = cell.getValue();
You can also simply this area:
while (allAgentsSheets.hasNext()){
var ss = SpreadsheetApp.openById(allAgentsSheets.next().getId());
var sheet = ss.getSheets()[1];
You don't need to use setActive and getActive.
I have 184 audit sheets in one folder. I want to reference one cell in each of these sheets and bring them back in to one master spreadsheet.
I have a code that does the opposite that sends a value to each sheet in the folder and changes it to the value that I want. So in essence I want to do the opposite of the script below:
function getdata() {
var files = DriveApp.getFolderById("1gbA2JI1DYNku7SQPaCq1Qk27hnbimPag").getFiles()
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Jan");
var sourcerange = sourcesheet.getRange('A3');
var sourcevalues = sourcerange.getValues();
var destsheet = shoot.getSheetByName('Front Sheet');
var destrange = destsheet.getRange('B5');
destrange.setValues(sourcevalues);
}
}
Your example code doesn't appear to match your explanation but I think I got the basic idea so I made up some of the needed details on my own. I included filenames, ids and 'A3' values. You can choose to modify as needed.
function getdata(month) {
var monthA=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var date=new Date();
var mss=SpreadsheetApp.getActive();
var ms=mss.getSheetByName("Master Sheet");
var vObj={name:[],id:[],value:[]};
var files = DriveApp.getFolderById("1gbA2JI1DYNku7SQPaCq1Qk27hnbimPag").getFilesByType(MimeType.GOOGLE_SHEETS);
while (files.hasNext()) {
var file = files.next();
var fid=file.getId();
var ss = SpreadsheetApp.openById(fid);
var sh = ss.getSheetByName(monthA[date.getMonth()]);
var rg = sh.getRange('A3');
vObj.name.push(file.getName());
vObj.id.push(fid);
vObj.value.push(rg.getValue());
vObj['month']=monthA[date.getMonth()];
}
vObj.id.splice(0,0,'ids');
ms.appendRow(vObj.id);
vObj.name.splice(0,0,vObj.month);
ms.appendRow(vObj.name);
vObj.value.splice(0,0,'values');
ms.appendRow(vObj.value)
}
I am using the below script to make a copy of my google worksheet (values and formatting only). However, this script is placing the new file in my main google drive and I want the file to be saved to an archive folder. How can I edit my script to do this?
function copySheetValuesV4(){
var sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheets = sourceSpreadsheet.getSheets();
var destination = SpreadsheetApp.create('03_'+sourceSpreadsheet.getName()+' _December 2017');
for (var i = 0; i < sourceSheets.length; i++){
var sourceSheet = sourceSheets[i];
if (!sourceSheet.isSheetHidden()) {
var sourceSheetName = sourceSheet.getSheetName();
var sValues = sourceSheet.getDataRange().getValues();
sourceSheet.copyTo(destination)
var destinationSheet = destination.getSheetByName('Copy of '+sourceSheetName).setName(sourceSheetName);
destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues);// overwrite all formulas that the copyTo preserved */
}
destination.getSheetByName("sheet1").hideSheet() // Remove the default "sheet1" */
}
}
Use DriveApp.getFolderById(folder_id).addFile(DriveApp.getFileById(destination.getId())). This gets the spreadsheet ID and then adds the spreadsheet to a folder.