Google Apps Script - Run Same Code on Dynamically Changing Tab Numbers - javascript

I would like the below code to run on how ever many tabs get created - after Sheet 7 (First 7 tabs always remain unchanged). Currently I use an array and must number them which works if you know exactly how many tabs get created - which I dont always know. So I currently create script for [7] then [8] etc etc. this does return an error when I say have [20] but Tab 20 doesnt exist.
function Company_ONE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[7]; //SHEET 8
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheets()[7];
var lr = sheet.getLastRow();
var cell = SpreadsheetApp.getActive().getRange('AK3');
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation()
.requireCheckbox()
.build();
cell.setDataValidation(rule);
sheet.getRange('AK3').activate();
var destinationRange = sheet.getActiveRange().offset(0, 0, lr-3);
sheet.getRange('AK3').copyTo(destinationRange);
}

Explanation:
Use getSheet().slice(7) to get from 8th sheet onwards. See here how slice works.
Then you can use forEach() to iterate through every sheet (after sheet 7th).
I also removed some unnecessary lines of codes. For example, you use SpreadsheetApp.getActive() multiple times in the sheet or you define the same variables twice like ss or sheet.
Since you are interacting with the sheets iteratively you might need to use SpreadsheetApp.flush() to make sure all the pending changes are completed.
Solution:
function Company_ONE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets().slice(7); // get 8th sheet onwards
sheets.forEach(sheet=>{
var lr = sheet.getLastRow();
var cell = ss.getRange('AK3');
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation()
.requireCheckbox()
.build();
cell.setDataValidation(rule);
sheet.getRange('AK3').activate();
var destinationRange = sheet.getActiveRange().offset(0, 0, lr-3);
sheet.getRange('AK3').copyTo(destinationRange);
SpreadsheetApp.flush();
});
}

Related

How do I fix a google script error that says coordinates of target range are outside the dimensions of the sheet when trying to archive? [duplicate]

I have been using a script in my google sheet that I found here:
copy data from one sheet to another in google app script and append a row, one small issue
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");
// get source range
var source = copySheet.getRange(2,2,12,2);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
}
When I am running it now it gives me an error:
The coordinates of the target range are outside the dimensions of
the sheet.
which wasn't coming up before. Does anyone have an idea why it is doing this?
Issue:
The error is in this line:
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
pasteSheet.getLastRow() returns a row number which is at the very bottom of your sheet. Then you consider 12 rows range but the pasteSheet does not have 12 rows after the last row with content.
Solutions:
Add more rows in the pasteSheet:
or you can use insertRowsAfter:
pasteSheet.insertRowsAfter(pasteSheet.getLastRow(), 12)
Code snippet:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");
// get source range
var source = copySheet.getRange(2,2,12,2);
// insert rows to make sure you have enough space
pasteSheet.insertRowsAfter(pasteSheet.getLastRow(), 12)
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
}

How to append data range to a list in a separate worksheet in Google Apps Script?

I've seen other approaches, but haven't been able to get anything working. I've been using a script similar to this to transfer a list from one TAB to another.
I thought I could just add in a different ID, set the variables, and then transfer it to a completely different WORKSHEET:
function archivesingle() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssd = SpreadsheetApp.openById('sheetid');
var idstoadd = ss.getActiveSheet();
var fulllist = ssd.getSheetByName("History");
Logger.log("Option#1 - Start")
// step 1 - get the last row on the target: Full List
var last_row = fulllist.getRange(1, 1, fulllist.getLastRow(), 1).getValues().filter(String).length;
Logger.log("Step#1 - the last row on fulllist = "+last_row)
// step 2 - create a target range (or at least the top left corner of the target)
var targetrange = fulllist.getRange(last_row+1,1);
Logger.log("Step#2 - the target range will be "+targetrange.getA1Notation())
// step 3 - create a source range
// how many header rows
var numheaderrows = 0
// so the range will be...
var sourcerange = idstoadd.getRange(1+numheaderrows, 14, idstoadd.getLastRow()-numheaderrows, 2);/// to pull in more columns, adjust last number
Logger.log("Stewp#3 - the sourcerange = "+sourcerange.getA1Notation());
// step4 - copy from source to target
// method = range.copyto
// so copyto from the source range to the target range
sourcerange.copyTo(targetrange,{contentsOnly:true});
Logger.log("Step#4 - copied the source range to the target range and flushed");
// step 6 - apply all the pending changes.
SpreadsheetApp.flush();
Logger.log("Step#6 - Flushed the spreadsheet")
Logger.log("Option#1 - Completed")
return false;
}
I was hit with the "target range and source range must be on the same spreadsheet" error.
I've seen other approaches, but I'm new to this and don't really understand how they work.
Is there a way to tweak this in order to accomplish my task (append a range of data to a list of data hosted in a separate worksheet)?
Any insight is much appreciated.
Try this:
function archivesingle() {
const ss = SpreadsheetApp.getActive();
const ssd = SpreadsheetApp.openById('sheetid');
const ish = ss.getActiveSheet();
const hsh = ssd.getSheetByName("History");
let ivs = ish.getRange(1, 14, ish.getLastRow(), 2).getValues()
hsh.getRange(hsh.getLastRow() + 1,1,ivs.length,ivs[0].length).setValues(ivs);
}

Google Sheets - Sending Row to Another Spreadsheet

Written a Google Sheet where when a button is pressed a collated row of data is sent to a different Google Sheet. It was launched six months ago among colleagues that have full permissions to both sheets. The reason for this post was that 1 in 10 submissions goes missing and never arrives to be added to the secondary Google Sheet. Thus wondering if anyone had any ideas as to if there was an issue with my code.
sh.getRange("j7").setValue('=now()');
var range = sh.getRange('J7:R7'); //Fixed range of where the row/data is collated
var data = range.getValues();
var tss = SpreadsheetApp.openById('SHEET ID');
var ts = tss.getSheetByName('Main');
ts.getRange(ts.getLastRow()+1,1,1,9).setValues(data);
Many thanks for any ideas...
Try this:
function lfunko() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Enter Your Sheet Name");
sh.getRange("J7").setFormula('=now()');
var range = sh.getRange('J7:R7');
var data = range.getValues();
var tss = SpreadsheetApp.openById('SHEET ID');
var ts = tss.getSheetByName('Main');
ts.getRange(ts.getLastRow() + 1, 1, data.length, data[0].length).setValues(data);
}

How do I Set a formula to value in Google scripts?

I have written a code that generates random numbers and concatenates them with text. I am using this function to fill a range of cells (from A1 to Z10):
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1:Z10");
cell.setFormula('=CONCATENATE("61", RANDBETWEEN(1000000000, 9999999999), "#text")');
}
The problem with this is that every time I reload the page, the numbers are recalculated, there is no static value. I want the values in my range to become static, so that every time I reload the page, the numbers in between the "61" and "#text" remain the same. I have tried the following two codes, and neither has worked. The first did not receive an error message, but simply did not keep the values static, while the second received the error message ""
function copyFormulasToValues() { var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Sheet1");
var destinationSheet = ss.getSheetByName("Sheet1");
var range = sourceSheet.getRange(1,1,1,1);
range
.copyValuesToRange(destinationSheet, 1, 1, range.getNumRows(), range.getNumColumns());
};
The second code:
var studentSheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet();
// copyTo rows
sourceSheet.getRange(1, 1, 10, 26).copyTo(studentSheet.getRange(1, 1, 10, 26), {contentsOnly:
true});
SpreadsheetApp.flush(); // applies all values to the student spreadsheet
// and therefore all the formula cells update
// get value from calculated formula cell
studentSheet.getRange(1, 1, 10, 26).copyTo(sourceSheet.getRange(1, 1, 10, 26), {contentsOnly:
true});
}
The error message:
Cannot find method getRange(number,number,number,number). (line 11, file "Code")
I just want to copy and paste the values in the same spreadsheet so they remain static. How can I achieve this?
NOTE 1.
I really appreciate the help Tanaike. I tried out the script (which I have posted below), but when I try and run it, the function runs the code, and then just entirely deletes the values.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1:Z10");
cell.setFormula('=CONCATENATE("61", RANDBETWEEN(1000000000, 9999999999),
"#text.com")');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange("A1:Z10");
range.copyValuesToRange(sheet, 1, range.getNumColumns(), 1,
range.getNumRows());
}
Perhaps I am doing something wrong?
You want to remove the formulas and put the values to the cells of A1:Z10 which is the same range.
The formulas has already been put to the Spreadsheet.
If my understanding is correct, how about this answer?
Pattern 1:
In this pattern, copyValuesToRange is used.
Sample script:
function copyFormulasToValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange("A1:Z10");
range.copyValuesToRange(sheet, 1, range.getNumColumns(), 1, range.getNumRows());
}
I think that in your script, the arguments might be wrong. Please set sheet, column, columnEnd, row, rowEnd in order.
Pattern 2:
In this pattern, copyTo is used.
Sample script:
function copyFormulasToValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getSheetByName("Sheet1").getRange("A1:Z10");
range.copyTo(range, {contentsOnly: true});
}
In this case, please use copyTo of Class Range.
References:
copyValuesToRange(sheet, column, columnEnd, row, rowEnd)
copyTo(destination, options)
If I misunderstood your question and this was not the direction you want, I apologize.

Hide all but today's columns on four sheets (Google Spreadsheets script)

Goal: I'm trying to create a behavior tracker for four classes in Google Spreadsheets. The tracker has nine sheets: Class7A, Class7B, Class8A, Class8B, and Mon-Fri summary sheets. The goal was for each ClassXX sheet to have behavior tracking information for an entire week, but for the default view to show only the current day's information.
Attempts: During initial workup (with only the Class7A sheet created), I got this to work using a modification of the script found here (Thank you Jacob Jan Tuinstra!): Optimize Google Script for Hiding Columns
I modified it to check the value in the third row of each column (which held a 1 for Monday, 2 for Tuesday, etc), and if it did not match the numerical equivalent for the day of the week (var d = new Date(); var n = d.getDay();), then it would hide that column. This process was somewhat slow - I'm assuming because of the iterating through each column - but it worked.
Quite excited, I went ahead and added the rest of the sheets, and tried again - but the code as written, seems to affect only the current sheet. I tried modifying it by replacing var sheet = ss.getSheets()[0]; with for script that iterated through the columns, until i>4 (I've since lost that piece of code), with no luck.
Deciding to go back and try adapting the original version of the script to instead explicitly run multiple times for each named sheet, I found the that script no longer seems to work at all. I get various version of "cannot find XX function in sheet" or "cannot find XX function in Range."
Source: A shared version (with student info scrubbed) can be found here: https://docs.google.com/spreadsheets/d/1OMq4a4_Gh_xyNk_IRy-mwJn5Hq36RXmdAzTzx7dGii0/edit?usp=sharing (editing is on).
Stretch Goal: Ultimately, I need to get this to reliably show only the current day's columns (either through preset ranges (same for each sheet), or the 1-5 values), and I need it to do so for all four ClassXX sheets, but not the summary pages (and preferably more quickly than the iterations). If necessary, I can remove the summary pages and set them up externally, but that's not my first preference. I would deeply appreciate any help with this; so far my attempts have seemed to only take me backwards.
Thanks!
Current code:
function onOpen() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// create menu
var menu = [
{name: "Show Today Only", functionName: "hideColumn"},
{name: "Show All Days", functionName: "showColumn"},
{name: "Clear Week - WARNING will delete all data", functionName: "clearWeek"}
];
// add to menu
ss.addMenu("Show Days", menu);
}
var d = new Date();
var n = d.getDay();
function hideColumn() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get first sheet
var sheet = ss.getSheets()[0];
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn()+1;
Logger.log(lastCol);
// itterate through columns
for(var i=1; i<lastCol; i++) {
if(data.getCell(2, i).getValue() != n) {
sheet.hideColumns(i);
}
}
}
function showColumn() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get first sheet
var sheet = ss.getSheets()[0];
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn();
// show all columns
sheet.showColumns(1, lastCol);
}
I cannot recreate the problem of the script not working at all, it's working fine for Class7A so that part is working fine.
So let's look at the two other problems:
Applying this to all Sheets
Speeding up the script
First let's create some globals we use in both functions
var d = new Date();
var n = d.getDay();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNames = ss.getSheets().map(function(sheet) {return sheet.getName();});
var classSheets = sheetNames.filter(function(sheetName) {return sheetName.match("Class")});
Now we can iterate over classSheets and get the sheet by name and hide columns in each.
However hiding each individual column is very slow.
The sheet is built very structured, every week has 12 columns (except for friday which doesn't have the grey bar), so we can just calculate the ranges we want to hide.
function hideColumn() {
classSheets.map(function(sheetName){
var sheet = ss.getSheetByName(sheetName);
if (n == 1) {
// Hide everything after the first three columns + Monday
sheet.hideColumns(3 + 11, 12 * 4);
} else if (n == 5) {
// Hide everything to the left except the leftmost three columns
sheet.hideColumns(3, 4 * 12);
} else {
// Hide everything left of the current day
sheet.hideColumns(3, (n - 1) * 12);
// Hide everything after the current day
sheet.hideColumns(3 + n * 12, (5 - n) * 12 - 1);
}
});
}
Lastly we can shorten showColumn
function showColumn() {
classSheets.map(function(sheetName){
var sheet = ss.getSheetByName(sheetName);
var lastCol = sheet.getLastColumn();
sheet.showColumns(1, lastCol);
});
}

Categories