I am looking for a little bit help on this situation. I have a spreadsheet where I create a new sheet, I rename it, and once this is done I do the same for like around 10 other spreadsheets, I would like to know how to implement a for or a while loop so that I dont have to type the repeated code 10 times. My code is below, any help would be really appreciate it. I have the keys in another document, so basically I need to know how to be able to read the keys in a variable and then be able to read each key since they are in a vertical column.
function create(){
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
ss0.setActiveSheet(ss0.getSheetByName("old"));
ss0.setActiveSheet(ss0.duplicateActiveSheet());
ss0.renameActiveSheet("main");
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
ss0.getRange("A4:I500").clearContent();
//repeated code key 1
var ss0 = SpreadsheetApp.getActiveSheet();
var master = SpreadsheetApp.openById("key1");
ss0.copyTo(master);
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
master.setActiveSheet(master.getSheetByName("Copy of main"));
master.renameActiveSheet("main");
// key 2
var ss0 = SpreadsheetApp.getActiveSheet();
var master = SpreadsheetApp.openById("key2");
ss0.copyTo(master);
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
master.setActiveSheet(master.getSheetByName("Copy of main"));
master.renameActiveSheet("main");
}
You need to get the keys in an array and loop over this array.
Here is an outline
function myFunction() {
//get the keys in an array
//say, they are in column A in a spreadsheet from A1:A10
var keyArray = SpreadsheetApp.openById("ID of the ss with key").getSheetByName("name of the sheet with key")
.getRange("A1:A10").getValues();
var ss0 = SpreadsheetApp.getActiveSheet();
//loop for all the spreadsheets
for(var i in keyArray){
var ss = SpreadsheetApp.openById(keyArray[i][0]);
ss0.copyTo(ss);
ss.getSheetByName("Copy of main").setName("main")
}
}
Related
I want eliminate formula information that is in an array of cells. My idea was to get the values of the cells as an array and then set the values back into the same place. I can't get it to work and have a feeling it due to my syntax. Any suggestions?
function getset() {
var ss = SpreadsheetApp.getActive();
var lr = sh.getLastRow();
var places = ss.getRange(2,11, lr-1).getValues();
ss.getRange(2,11, lr-1).setValues(places);
}
This works for me
function getset() {
var ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
var lr = sh.getLastRow();
var places = sh.getRange(2,11, lr-1).getValues();
sh.getRange(2,11, lr-1).setValues(places);
}
So this might be a simple one for someone more skilled. The code works except it copies the functions rather than the values. The cells contain a LOT of functions and I'm using this to create a second version since the functions are UI data reliant and this is the easiest way to make a "Backup" in case we need to go back to an old forms data. I'm using the below code and it copies everything over, and creates the sheet with the right name, but I'm not sure how to get it to take the values and not the functions.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("AHA");
var nameSheet = sourceSheet.getRange(1, 15).getValue();
var target = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
var targetSheet = sourceSheet.copyTo(target);
targetSheet.setName(nameSheet);
I've tried this,
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("AHA");
var nameSheet = sourceSheet.getRange(1, 15).getValue();
var target = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
var targetSheet = sourceSheet.copyTo(target,SpreadsheetApp.CopyPasteType.PASTE_VALUES);
targetSheet.setName(nameSheet);
but it gives me the following error:
Exception: The parameters (SpreadsheetApp.Spreadsheet,SpreadsheetApp.CopyPasteType) don't match the method signature for SpreadsheetApp.Sheet.copyTo.
Any help or advice on another script I could use would be appreciated. Thanks.
Try this:
function copyvalues() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("AHA");
const vs = sh.getDataRange().getValues();
const name = sh.getRange(1, 15).getValue();
const tss = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
const tsh = tss.insertSheet(name);
tsh.getRange(1,1,vs.length,vs[0].length).setValues(vs);
}
I believe this will be a super easy question for you. I downloaded a YT script to automatically update YT views on Google Sheets which you can find here (https://developers.google.com/gsuite/solutions/youtube-tracker).
I have a spreadsheet with 12 tabs and the code has a function to go through every tab in the spreadsheet.
function markVideos() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// Runs through process for each tab in Spreadsheet.
sheets.forEach(function(dataSheet) {
var tabName = dataSheet.getName();
var range = dataSheet.getDataRange();
var numRows = range.getNumRows();
var rows = range.getValues();
var headerRow = rows[0];
I want to change it to apply to a specific tab only, let's say "Sheet12", instead of running it for every tab.
function markVideos() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet12");
// Runs through process for each tab in Spreadsheet.
function NameOfTheFunction(dataSheet) {
var tabName = dataSheet.getName();
var range = dataSheet.getDataRange();
var numRows = range.getNumRows();
var rows = range.getValues();
var headerRow = rows[0];
I tried this but had no luck because it doesn't seem to be applying the function to the specific sheet. I believe this is super easy fix but I have no idea about programming.
Remove the forEach statement and use getSheetByName instead of getSheets:
function markVideos() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tabName = 'Sheet12';
var dataSheet = ss.getSheetByName(tabName)
var range = dataSheet.getDataRange();
var numRows = range.getNumRows();
var rows = range.getValues();
var headerRow = rows[0];
}
Array ForEach() Method calls a function once for each element in an array. That is why a function must be defined that will take an array element function(dataSheet)
Since you only need to access 1 sheet based on its sheet name.
You don't need to define a function to access the properties of that sheet.
Sample:
Sample Code:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheetByName("Sheet1");
var tabName = sheets.getName();
var range = sheets.getDataRange();
var numRows = range.getNumRows();
var rows = range.getValues();
var headerRow = rows[0];
Logger.log(tabName);
Logger.log(numRows);
Logger.log(rows);
Logger.log(headerRow);
}
Logger Log Output:
References:
Class Sheet Methods
SpreadsheetApp Methods
I am new to this and I am having some trouble trying to figure this out. I have a spreadsheet that collects data from a google form. I am trying to find a way to move that data based on a column answer to a different google sheet. (Not a sheet in the same document but a different document all together). It seems like there is some information about moving to a different tab in the same document, but not a different document.
I will first say that I tried just an IMPORTRANGE function, but that only mirrors the data, and does not let you update or change cells.
Here is what I have been able to piece together so far, but I may be way off.
I have a trigger that would run every hour.
function myFunction() {
var ss = SpreadsheetApp.openById('1U0I9SkbGkHgm-vRkwf2Ppc_yxlqrVlg2t8yKRy3sYuI');
var sheetOrg = ss.getSheetByName("Form Responses 1");
var value1ToWatch = "ANDERSON";
var value2ToWatch = "BARNES";
var sheetNameToMoveTheRowTo = "Sheet1"; //sheet has same name for each target openByID(" *url key* ")
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ts1 =SpreadsheetApp.openById("1PxV1PQrMdu_2aSru4dpan8cUgHYdlYPUp5anyzjMAms");
sheet1in = ts1.getSheetByName("Sheet1");
var ts2 = SpreadsheetApp.openById("1BYyQZNiXc2QqsyqWs7uazjl5B6mfFYM1xj3u8gWyYOQ");
sheet1in = ts2.getSheetByName("Sheet1");
arr = [],
values = sheetOrg.getDataRange().getValues(),
i = values.length;
while (--i) {
if (value1ToWatch.indexOf(values[i][1]) > -1) {
arr.unshift(values[i])
sheetOrg.deleteRow(i + 1)
sheet1in.getRange(sheet1in.getLastRow()+1, 1, arr.length,
arr[0].length).setValues(arr);
};
if (value2ToWatch.indexOf(values[i][1]) > -1) {
arr.unshift(values[i])
sheetOrg.deleteRow(i + 1)
sheet2in.getRange(sheet2in.getLastRow()+1, 1, arr.length,
arr[0].length).setValues(arr);
};
}
}
The sheet google forms dumps the information into is "Form Responses 1".
Column B is the cells I want to get the values from. (There are a total of 9 different values that it can be like "ANDERSON", "BARNES", "SMITH", etc).
For sheetNameToMoveTheRowTo is "Sheet1" - that may be confusing and I may need to change that, but for example
ts1 =SpreadsheetApp.openById("1PxV1PQrMdu_2aSru4dpan8cUgHYdlYPUp5anyzjMAms") the sheet name that I want the information moved to is "Sheet1".
ts2 = SpreadsheetApp.openById("1BYyQZNiXc2QqsyqWs7uazjl5B6mfFYM1xj3u8gWyYOQ") the sheet name is also "Sheet1" but in a different document.
I think if I were able to get the "ANDERSON" one to work, then I can just add additional variables for each possible response, and just copy and paste additional "IF" statements, just changing the valueToWatch and targetSheet values. <= if that is not correct please let me know
I have tried to both debug, and run the script above but nothing happens. There are no errors reported on the debug, but it is not moving any information over.
Any idea what I am doing wrong?
// UPDATE I got this to work. I have updated the code listed with what worked for me.
I think that copyTo() method will not work like you mentioned, it operates on same SpreadSheet. I'm sending you example with looping on source sheet data and then setting the target sheet values with it.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var allrange = ss.getActiveRange();
var range = sheet.getRange("A1:A10");
var values = range.getValues();
var allvals = [];
for(var i = 0; i < values.length; i++) {
allvals.push( values[i] ) ;
}
var dataLength = allvals.length;
// alert data
var ui = SpreadsheetApp.getUi();
// ui.alert( JSON.stringify(allvals) )
// copy to new Google SpreadSheet
var newSheet = SpreadsheetApp.openById('1F79XkNPWm2cCWlB2JP3K4tAYRESKUgtHK4Pn2vbJEiI').getSheets()[0];
var tmp = "A1:A" + dataLength ;
var newRange = newSheet.getRange( tmp );
newRange.setValues( allvals );
}
Here's a simple example of moving data from one spreadsheet to another.
function movingDataToSS(){
var ss=SpreadsheetApp.getActive();
var dss=SpreadsheetApp.openById('ssId');
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var vA=rg.getValues();
var dsh=dss.getSheetByName('Sheet1');
var drg=dsh.getRange(1,1,rg.getHeight(),rg.getWidth()).setValues(vA);
}
If you interested in placing some conditions on your output by only getting output from odd rows and even columns.
function movingDataOddRowsAndEvenCols(){
var ss=SpreadsheetApp.getActive();
var dss=SpreadsheetApp.openById('ssId');
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var vA=rg.getValues();
var h=rg.getHeight();
var w=rg.getWidth();
var dsh=dss.getSheetByName('Sheet1');
for(var i=0;i<h;i++){
var out=[];
if(i%2==0){
for(var j=0;j<w;j++){
if(j%2==1){
out.push(vA[i][j]);
}
}
dsh.appendRow(out);
}
}
}
I have a google spreadsheet that contains around 50 to 60 sheets. I am trying to get the names of all those sheets, and put them into another spreadsheet.
So far I have written the following code
function myfunction(){
var ss = SpreadsheetApp.openById("abck12345");
var sheetCount = ss.getSheets().length;
var i=0;
var sheetlist;
while(i<sheetCount){
sheetlist = ss.getSheets()[i];
Logger.log(sheetlist);
}
}
While executing this i am getting an error "Exceeded maximum execution time"
It is more efficient to get all the sheets at once. Your code is getting all the sheets on every loop. This code gets all the sheet names and puts them into a new spreadsheet:
function myfunction() {
var allSheets,i,sheetCount,sheetlist,sheetName,ss,
targetSh,targetSS,thisSheet;
ss = SpreadsheetApp.openById("abck12345");
targetSS = SpreadsheetApp.openById("999999");
allSheets = ss.getSheets();
sheetCount = allSheets.length;
sheetlist = [];
for (i=0;i<sheetCount;i++) {
thisSheet = allSheets[i];
sheetName = thisSheet.getName();
sheetlist.push([sheetName]);//Put an inner array of each sheet tab name into
//an outer array named sheetlist
//Creates a 2D array for setting the values later
}
Logger.log(sheetlist);
targetSh = targetSS.getSheetByName('target sheet tab name');
targetSh.getRange(targetSh.getLastRow()+1, 1, sheetlist.length, sheetlist[0].length).setValues(sheetlist);
}
You are not incrementing i. A better way to do would be as follows:
function myfunction(){
var ss = SpreadsheetApp.openById("1cmUWU_nbE6idJUmpNtKnhq0KUekVXnW9OYVZr93TSNs");
var sheets = ss.getSheets();
var i;
for(i in sheets){
Logger.log(sheets[i].getName())
}
}