I have a list of row numbers in a spreadsheet which I need to change the background colour of. As the spreadsheet is quite large (10+ sheets, each with almost 5000 rows), I am trying to construct a range so I can batch set the background, as doing each row individually was taking over the max time of 6 minutes.
Here's the code I have:
// highlight required rows
var first = -1, last = -1;
for(var j = 0; j < rowNumsToHighlight.length; j++) {
if(first == -1) {
first = rowNumsToHighlight[j];
continue;
}
// if the current row number is one more than the previous, update last to be the current row number
if(rowNumsToHighlight[j] - 1 == rowNumsToHighlight[j - 1]) {
last = rowNumsToHighlight[j];
continue;
}
// otherwise the last row should be the previous one
else {
last = rowNumsToHighlight[j - 1];
}
var numRows = (last - first) + 1;
var range = sheet.getRange(first, 1, numRows, 4);
if(range.getBackground().toUpperCase() != highlightColour.toUpperCase()) {
range.setBackground(highlightColour);
}
first = -1;
last = -1;
}
rowNumsToHighlight is just an array that looks like: [205,270,271,272,278,279]. So, with that as an example, setBackground should be ran on row 205, on rows 270-272, and on 278-279.
I'm fairly sure the solution is simple, but just can't see it. Thanks for any help.
==== Updated Code ====
Based on Serge's code below, I made it more efficient again by reducing the number of getRange() calls made. Time is down from 78 to 54 seconds.
function updateColours(sheet, array, colour){
var columns = sheet.getLastColumn();
var rows = sheet.getLastRow();
var range = sheet.getRange(1, 1, rows, columns);
Logger.log("Resetting highlight on all rows...");
range.setBackground(null);
var backgrounds = range.getBackgrounds();
for(var n = 0; n < backgrounds.length; n++){
var rowIdx = n + 1;
if(array.indexOf(rowIdx) > -1){
for(var c = 0; c < columns; c++){
backgrounds[n][c] = colour;
}
}
}
Logger.log("Highlighting non-translated rows...");
range.setBackgrounds(backgrounds);
}
Maybe this one is faster(?) and built in a way that will make your work easier (function with arguments).
It writes only once to the sheet (or 2 if you clear colors before writing)...
use like below :
function testBG(){
updateColors(0,[7,8,9,18,19,23]);
}
function updateColors(sheetNum,array){
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets()[sheetNum];
var columns = sh.getMaxColumns();
var range = sh.getRange(1,1,sh.getMaxRows(),columns);
sh.getRange(1,1,sh.getMaxRows(),columns).setBackground(null);// use this if you want to clear all colors before setting them
var backGrounds = range.getBackgrounds();// get all cells BG
for(var n=0;n<backGrounds.length;n++){
var rowIdx = n+1;
if(array.indexOf(rowIdx)>-1){
for(c=0;c<columns;c++){
backGrounds[n][c]="#F00";// if row number is in the array fill in red
}
}
}
sh.getRange(1,1,sh.getMaxRows(),columns).setBackgrounds(backGrounds);//update sheet in one call
}
test sheet in view only, make a copy to test.
This is how I would do it:
function createRanges() {
var rowNumsToHighlight = [5,7,8,9,18,19];
var arrayLength = rowNumsToHighlight.length;
var loopCounter = 0, thisNumberInArray=0, nextNumberInArray=0, crrentNmbrPlusOne=0;
var currentRangeBegin=0, numberOfRowsInRange=1;
currentRangeBegin = rowNumsToHighlight[0];
for(loopCounter=0; loopCounter < arrayLength; loopCounter+=1) {
thisNumberInArray = rowNumsToHighlight[loopCounter];
nextNumberInArray = rowNumsToHighlight[loopCounter+1];
crrentNmbrPlusOne = thisNumberInArray+1;
if (nextNumberInArray===undefined) {
workOnTheRange(currentRangeBegin, numberOfRowsInRange);
return;
};
if (nextNumberInArray!==crrentNmbrPlusOne) {
workOnTheRange(currentRangeBegin, numberOfRowsInRange);
numberOfRowsInRange = 1; //Reset to 1
currentRangeBegin = nextNumberInArray;
} else {
numberOfRowsInRange+=1;
};
};
};
function workOnTheRange(first,numRows) {
var range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet11').getRange(first, 1, numRows, 4);
range.setBackground("red");
};
I've tested the code and it works.
Related
I have a range with data where I need a script to look for duplicates in the first column, and clear contents of the hole row in that range.
From this
To this
I've found this script, but it deletes the whole row. I need it to only clear content.
function ClearDuplicates() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("03");
var rows = sheet.getLastRow();
var firstColumn = sheet.getRange("A:A").getValues();
for (var i = rows; i >= 2; i--) {
if (firstColumn[i-1][0] == firstColumn[i-2][0]) {
sheet.deleteRow(i);
}
}
}
You need to retrieve the array data from column A and iterate through it comparing with the same array. When a match happens, apply clearContent() function [1] to clear the contents on that range (First get the range you want to clear in that row).
function ClearDuplicates() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var rows = sheet.getLastRow();
var firstColumnValues = sheet.getRange("A1:A" + rows).getValues();
for (var i = 0; i < firstColumnValues.length; i++) {
var value = firstColumnValues[i][0];
for (var j = 0; j < firstColumnValues.length; j++) {
var comparisonValue = firstColumnValues[j][0];
if ((value == comparisonValue) && (j>i)) {
//You need to change the number of columns to which you need to clear the contents, in this case is 10
sheet.getRange(j+1, 1, 1, 10).clearContent();
}
}
}
}
[1] https://developers.google.com/apps-script/reference/spreadsheet/range#clearcontent
You can just grab the range and clear it instead as seen below:
sheet.getRange("R"+i+"C").clear();
How can I repeat 'SAT' 10 times in a row and then 'SUN' times in a row more efficiently?
I know I could use copy/paste, click/drag, custom functions (like this) and combinations of built-in functions like TRANSPOSE(SPLIT(TRIM(REPT(). Those are great if you don't have to use them often. However, with more than just 'SAT' and 'SUN' as values, that quickly becomes cumbersome.
I asked a similar question a few days ago. Working off the solutions provided by dev1998 and Serge insas, I created a working script below.
function testAtA1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('TEST'));
// start columns and rows
var currentRow = 1;
var startColumn = 1;
var numRows = 1; // Change to match number of items listed for a given variable
var numColumns = 1;
var day1 = [
['SAT']
]
var day2 = [
['SUN']
]
Logger.log(day1);
Logger.log(day2);
var day1Length = day1.length;
var day2Length = day2.length;
Logger.log(day1Length);
Logger.log(day2Length);
var currentRow2 = currentRow + 10
// ranges where values will be placed
for (i = 0; i < 10; i++) {
var target1 = sheet.getRange(currentRow, startColumn, numRows, numColumns).setValues(day1);
currentRow = currentRow + 1; // Change to match number of items listed for a given variable
}
for (i = 0; i < 10; i++) {
var target2 = sheet.getRange(currentRow2, startColumn, numRows, numColumns).setValues(day2);
currentRow2 = currentRow2 + 1
}
}
In addition to what I have above, I also tried
var days = [['SAT'],['SUN']]
I was able to get that to alternate SAT and then SUN X times, but not get all SAT in a row and all SUN in a row. With the first snippet I provided I was able to separate them but, I'm curious to know if there is an easier/better way.
function testAtA1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('Sheet1'));
// start columns and rows
var currentRow = 1;
var startColumn = 1;
var numRows = 10; // Change to match number of items listed for a given variable
var numColumns = 1;
var days = ['SAT','SUN','MON']
for (d = 0;d < days.length ; d++) {
var data = days[d]
var target1 = sheet.getRange(currentRow, startColumn, numRows, numColumns).setValue(data);
currentRow = currentRow+10
}
}
By adding another variable d and making a another loop with d
lesser than days.length , We have a desired solution.
=ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(QUERY(REPT(D1:D7&"😋",10),,9^99),"😋"))))
D1:D7 will contain SAT through FRI
Assuming you just want to append the rows to the spreadsheet, this will do the trick:
function addText(){
var ss = SpreadsheetApp.getActive().getActiveSheet();
var satRow = [];
for (var i = 0; i < 10; i++){
satRow.push("SAT");
}
var sunRow = [];
for (i = 0; i < 10; i++){
sunRow.push("SUN");
}
ss.appendRow(satRow);
ss.appendRow(sunRow)
}
appendRow() takes a 1-dimensional array as an argument. So just build a 1D array and append it.
If you need to insert the rows at an arbitrary point, you're probably better off going with a custom function:
/**
* Fills a range specified in rows and columns with the specified value
*
* #param {number} Number of rows to fill
* #param {number} Number of columns to fill
* #param {string} input The value to iterate.
* #return An array containing the values
* #customfunction
*/
function fillRange(rows,cols,str){
var output = [[]];
for (var i = 0; i < cols; i++){
output[0].push(str);
}
for (var i = 1; i < rows; i++){
output.push(output[0]);
}
return output;
}
To use it, type in a cell =fillRange(1,2,"SAT") to fill 2 columns in one row with "SAT".
again a more efficient solution using array and bulk read/write as recommended in best practice...
function testAtA1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('TEST');
// start columns and rows
var currentRow = 1;
var startColumn = 1;
var numRows = 1; // Change to match number of items listed for a given variable
var numColumns = 1;
var day1 = 'SAT';
var day2 = 'SUN';
var data = sheet.getRange(1,1,sheet.getMaxRows(),sheet.getMaxColumns()).getValues();
var currentRow2 = currentRow + 10 ;// ranges where values will be placed
for (i = 0; i < 10; i++){
data[i][0] = day1;
data[i+10][0] = day2;
}
sheet.getRange(1,1,data.length,data[0].length).setValues(data);
}
I want to check for new names in a top 30 ranking from an API that refreshes daily, and then append every new name to an other column if it isn't already in there.
I think a for-loop would be the solution. This is what I got so far.
function appendValues(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var top30Names = ss.getRange("A4:A33").getValues();
var eligibleNames = ss.getRange("P4:P300").getValues();
for (i = 0; i < 30; i++){
var searchKey = top30Names[i]; // search if the eligible name is in the top30names
if (isInArray(searchKey, eligibleNames)){
// do nothing
}
else{
getFirstEmptyRow();
ss.getActiveCell().setValue(searchKey);
}
}
}
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
function getFirstEmptyRow() {
var sheet = SpreadsheetApp.getActiveSheet(),
values = sheet.getRange("P4:P300") // the range to search for the first blank cell
.getValues(),
row = 0; //start with the first array element in the 2D array retrieved by getValues()
for (row; row < values.length; row++) {
if (!values[row].join("")) break;
}
return sheet.setActiveSelection("P" + (row + 4)).getRow();//.getLastRow() // column between "" and row + starting_row in range
}
This appends the full top 30 each time, but I only need the new values.
I've found a work around using setFormula. If anyone has a more elegant solution, I'd be happy learn.
function appendNewName(){
setFormula();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var newNames = ss.getRangeByName("newEntries").getValues();
for (i = 0; i < 30; i++){
getFirstEmptyRow();
var x = newNames[i][0];
// Logger.log(x); // What does this do???
if (x.length > 1) {
ss.getActiveCell().setValue(newNames[i]);
}
}
}
function setFormula(){
// first run this
clearFormula();
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getRangeByName("newEntries").setFormula("=IF(ISNUMBER(MATCH(A4,AppendNew,0)),\"\",A4)"); // Sets a formula to the range that will show the new daily entries in top 30
}
function clearFormula(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getRangeByName("newEntries").clear();
}
function getFirstEmptyRow() {
var sheet = SpreadsheetApp.getActiveSheet(),
values = sheet.getRange("appendNew") // the range to search for the first blank cell
.getValues(),
row = 0; //start with the first array element in the 2D array retrieved by getValues()
for (row; row < values.length; row++) {
if (!values[row].join("")) break;
}
return sheet.setActiveSelection("P" + (row + 4)).getRow(); // column between "" and row + starting_row in range
}
I need to generate 5 random number within a specific range from 1 to 100 with out duplicates.
A1 = 1(from)
A2 = 100(to)
A3 = 5 (Required random number)
A4,A5,A6,A7,A8 in cell should generate random number
A simple way could be:
Generate a list of the 100 numbers
Shuffle the list using the Fisher-Yates algorithm
Take the first 5 numbers
There are faster ways, but for only 100 integers it should be fine.
Edit: Try this code:
function shuffleArray(array) { // from http://stackoverflow.com/a/12646864/5710637
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function Randomnumber() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("c2:C5");
var min = sheet.getRange("A1").getValue();
var max = sheet.getRange("A2").getValue();
var numbers = []
for (var i = min; i <= max; i++) {
numbers.push(i);
}
shuffleArray(numbers)
var counter = 0;
for (var x = 1; x <= range.getWidth(); x++) {
for (var y = 1; y <= range.getHeight(); y++) {
range.getCell(y, x).setValue(numbers[counter]);
counter++;
}
}
};
As fafl pointed you can use a list.
Generate list according to Range
Pop n Numbers out of them randomly one by one
Write popped Numbers to sheet
Here is an example.
/*Note: The Code does not have validations like the random number needed should be less
than the range etc. You should take care of such issues and improvise the code for the
same.
Rest of the code is optimized and makes a single read and write from Spread Making it
run fast*/
function myFunction() {
//var ss = SpreadsheetApp.getActiveSpreadsheet();
var ss = SpreadsheetApp.openById("1_xoBxknhDm1pM3MBw0Jbat3BTV4HXep7nZlOPw4tEWg");
var sheet = ss.getSheets()[0];
var values = sheet.getRange(1, 2, 3, 1).getValues();
var nRandomNumbers = getNRandomNumbers(values[0][0], values[1][0], values[2][0]);
sheet.getRange(4,2,values[2][0],1).setValues(nRandomNumbers);
}
function getRandomNumber(min, max) {
return Math.random() * (max - min) + min;
}
function getNRandomNumbers(from, to, n){
var listNumbers = [];
var nRandomNumbers = [];
for(var i = from; i <= to; i++) {
listNumbers.push(i);
}
for(var i = 0; i < n; i++) {
var index = getRandomNumber(0, listNumbers.length);
nRandomNumbers.push([listNumbers[parseInt(index)]]);
listNumbers.splice(index, 1);
}
return nRandomNumbers;
}
Demo Link:(Please Copy the code into your drive/sheet, can't get the permission working)
Script: https://script.google.com/d/1hsWiGCFZ3DlxiSB3ysTr5ThWvDzThS-vBVzHrJCIEW8zM4_DzndCwGkQ/edit?usp=sharing
Sheet: https://docs.google.com/spreadsheets/d/1_xoBxknhDm1pM3MBw0Jbat3BTV4HXep7nZlOPw4tEWg/edit#gid=0
I know this has been asked before because I have spent the last 5 hours trying them all from here and even the closed google product forum. I can't get any of the loops to stop. The conditions are getting met, but the loop keep running and running. I have been copying from other examples online and inserting my own variables, but in the end, I'll have 5 rows go to the new sheet several times until the sheet gets tired or something.
I've tried getDataRange...I've tried getRange...I've tried var in loops and other loops, I've tried them all, so please take a look and let me know how this code keeps resulting in rows getting added to the new sheet multiple times over:
function runReportAllMemCos1() {
var sheet1 = sskey.getSheetByName('Businesses');
var sheet2 = sskey.getSheetByName('tempsheet');
var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();
var dest = [];
for (var i = 0; i < data.length; i++ ) {
if (data[i][12] == "Associate") {
dest.push(data[i]);
} Logger.log(data)
if (dest.length > 0 ) {
sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
}
}
}
Thanks for any help!
I don't really agree with user1795832's answer as it doesn't make use of batch writing but simply writes data to sheet 2 each time the condition is true... so why using an array to do that ?
Try simply to batch write to sheet 2 after the loop is completed and it should work as expected.
function runReportAllMemCos1() {
var sheet1 = sskey.getSheetByName('Businesses');
var sheet2 = sskey.getSheetByName('tempsheet');
var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();
var dest = [];
for (var i = 0; i < data.length; i++ ) {
Logger.log(data[i][12]);// just to check if the condition is true sometimes ;-)
if (data[i][12] == "Associate") {
dest.push(data[i]);
}
} // here is the end of the for loop
Logger.log(dest) ; // log the dest array instead
if (dest.length > 0 ) { // if something has been written in your array then batch write it to the dest. sheet
sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
}
}
EDIT : other possibility following your comment to choose the columns you copy
function runReportAllMemCos1() {
var sheet1 = sskey.getSheetByName('Businesses');
var sheet2 = sskey.getSheetByName('tempsheet');
var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();
var dest = [];
for (var i = 0; i < data.length; i++ ) {
Logger.log(data[i][12]);// just to check if the condition is true sometimes ;-)
if (data[i][12] == "Associate") {
var destRow = []; // initialise intermediate array
destRow.push(data[i][1],data[i][2],data[i][12]);// choose here the columns you want to add (here col2, 3 & 13)
dest.push(destRow);
}
} // here is the end of the for loop
Logger.log(dest) ; // log the dest array instead
if (dest.length > 0 ) { // if something has been written in your array then batch write it to the dest. sheet
sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
}
}
Yep, the problem is your dest variable. You aren't resetting it after each loop, so each row just keeps getting compounded in there and duplicates each time (so when looping through the third row, rows 1 and 2 are still in the dest variable). Put "dest = [];" just inside your for loop and it'll work.
function runReportAllMemCos1() {
var sheet1 = sskey.getSheetByName('Businesses');
var sheet2 = sskey.getSheetByName('tempsheet');
var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();
var dest = [];
for (var i = 0; i < data.length; i++ ) {
dest = [];
if (data[i][12] == "Associate") {
dest.push(data[i]);
} Logger.log(data)
if (dest.length > 0 ) {
sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
}
}
}