Conditional formatting script - set iteration - google-sheets

I have a custom formula for conditional formatting rules. I am trying to write a script that checks a number of values (around 50) on a column (column B on 'Mine' sheet) and if a cell is equal to a specific string (M1, M2 or M3) then the specified formula for conditional formatting is applied to the "Calendar view" sheet. The code I currently have is:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Calendar View");
sheet.getRange("C4:NC50").clearFormat();
var range = sheet.getRange("C4:NC4");
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied('=AND(indirect("Mine!$B5")="M1", C$2>=indirect("Mine!$C5"), C$2<indirect("Mine!$D5"))')
.setBackground("#FF0000")
.setRanges([range])
.build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
How can I enter an iteration method on the .whenFormulaSatisfied, such as:
.whenFormulaSatisfied('=AND(indirect("Mine!$B6")="M1", C$2>=indirect("Mine!$C6"), C$2<indirect("Mine!$D6"))')
.whenFormulaSatisfied('=AND(indirect("Mine!$B7")="M1", C$2>=indirect("Mine!$C7"), C$2<indirect("Mine!$D7"))')
.whenFormulaSatisfied('=AND(indirect("Mine!$B8")="M1", C$2>=indirect("Mine!$C8"), C$2<indirect("Mine!$D8"))')
............
This is the sheet I'm working on:
https://docs.google.com/spreadsheets/d/1Af84aHaG0VjXmtaWc0-uAdGFrX1LozRNLQLMatSOqgU/edit?usp=sharing

There are some challenges to the questioner's methodology - first, the dynamic identification of the start and end dates for each property, and second, the creation of up to 50 separate Conditional Formatting rules. It's well known that spreadsheet performance is affected by numbers of Conditional Formatting rules.
I'm suggesting a slightly different approach.
1) Take the data on Mine and build the Calendar.
2) Place values in the booked date fields.
3) Apply a single Conditional Formatting rule for Calendar.
The methodology for identifying which dates are booked is to insert a nominal value in the respective cells. Then the rule .whenCellNotEmpty() is applied rather than specifying a specific value. In addition, the code formats both the background as well as the font colour so that any data is hidden.
Also to note: at the beginning of the script, the code removes both the content as well as the formatting.
function so_53185335() {
// build the spreadsheet app and set source and target sheets
var ss = SpreadsheetApp.getActiveSpreadsheet();
var calSheet = ss.getSheetByName("Calendar View");
var dataSheet = ss.getSheetByName("Mine");
// get the last rows and start rows for both sheets
var lrMine = dataSheet.getLastRow();
var lrCal = calSheet.getLastRow();
var dataRowStart = 5;
var calRowStart = 4;
// clear formats and data from Calendar
calSheet.getRange(calRowStart, 2, lrCal, 366).clear({
contentsOnly: true,
formatOnly: true
});
// get Mine rows with data, define the range and get data
var dataRows = lrMine - dataRowStart + 1;
//Logger.log("Mine: number of data rows "+dataRows);// DEBUG
var dataRange = dataSheet.getRange(dataRowStart, 2, dataRows, 3);
//Logger.log("data range is "+dataRange.getA1Notation());// DEBUG
var dataValues = dataRange.getValues();
//set some variables for use in loop
var i = 0; // counter
var z = 0; // counter
var calstartCol = 3; // equals first day of the year
var calrow = 0; // counter row for Calendar sheet
var calArray = [];
var masterArray = [];
// loop through the rows in Mine
for (i = 0; i < dataRows; i++) {
// test for value
if (dataValues[i][0] === "M1" || dataValues[i][0] === "M2" || dataValues[i][0] === "M3") {
//Logger.log("Match: i="+i+", value = "+dataValues[i][0]);//DEBUG
calArray = [];
masterArray = [];
calrow = calrow + 1;
// calculate the start day (as a day in the year)
var now = new Date(dataValues[i][1]);
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var startday = Math.floor(diff / oneDay);
// calculate the end day (as a day in the year)
var fnow = new Date(dataValues[i][2]);
var fstart = new Date(fnow.getFullYear(), 0, 0);
var fdiff = (fnow - fstart) + ((fstart.getTimezoneOffset() - fnow.getTimezoneOffset()) * 60 * 1000);
var foneDay = 1000 * 60 * 60 * 24;
var endday = Math.floor(fdiff / foneDay);
var nod = endday - startday + 1;
// assign the value for the Property
var cell = calSheet.getRange(calstartCol + calrow, 2);
cell.setValue(dataValues[i][0]);
// create an array of values for booked dates; just insert the number "1"
for (z = 1; z < nod + 1; z++) {
calArray.push(1);
}
masterArray.push(calArray);
// Assign the values for booked dates
var cell = calSheet.getRange(calstartCol + calrow, startday + 2, 1, nod);
cell.setValue(masterArray);
}
}
// create and apply a single Conditional forma rule for the data range on Calendar
var range = calSheet.getRange(calRowStart, calstartCol, calstartCol + calrow, 366);
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenCellNotEmpty()
.setFontColor("#FF0000")
.setBackground("#FF0000")
.setRanges([range])
.build();
var rules = calSheet.getConditionalFormatRules();
rules.push(rule);
calSheet.setConditionalFormatRules(rules);
}
The Calendar looks like this.

Related

Jump to a specific cell based on Active Cell

I'm completely new to Java Scipt although over the years I've messed around with VBA & Macros in Excel. I am now using Google Sheets almost exclusively, hence needing to learn Java.
I'm trying to jump to a specific cell (E5) if the current cell is (C14). This is what I've put together so far using scripts 'borrrowed' from others.
ie On entry of data in Cell C13 and pressing Enter, focus goes to Cell C14. The next data is to go into Cell E5.
function onSelectionChange(e) {
var sheetNames = ["Score Input"]; // Set the sheet name.
var ranges = ["C14"]; // Set the range to run the script.
var range = e.range;
var sheet = range.getSheet();
var check = ranges.some(r => {
var rng = sheet.getRange(r);
var rowStart = rng.getRow();
var rowEnd = rowStart + rng.getNumRows();
var colStart = rng.getColumn();
var colEnd = colStart + rng.getNumColumns();
return (range.rowStart >= rowStart && range.rowEnd < rowEnd && range.columnStart >= colStart && range.columnEnd < colEnd);
});
if (check) {
jumpToDetails();
}
};
function jumpToDetails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Score Input");
var goToRange = sheet.getRange('c14').getValue();
//sheet.getRange(goToRange).activate();
SpreadsheetApp.getActive().getRange('E5').activate();
}
It worked, or did until I inserted a row in the sheet, and even though I have changed the associated cell addresses, it now doesn't work?
Two questions. 'Why has it stopped working'? and 'Is there a simpler way to do it'?
I prefer using onEdit(e) on range C13 and jump to E5
For instance here is a script that jump from one cell to the following in addresses'list
function onEdit(event){
var sh = event.source.getActiveSheet();
var rng = event.source.getActiveRange();
if (sh.getName() == 'Score Input'){ // adapt
var addresses = ["C13","E5","E10","H10","E13","H13","E16"]; // adapt
var values = addresses.join().split(",");
var item = values.indexOf(rng.getA1Notation());
if (item < addresses.length - 1){
sh.setActiveSelection(addresses[item + 1]); // except last one
}
}
}
if you want to be able to add rows and columns, play with named ranges (for instance ranges names 'first', 'second, 'third')
function onEdit(event){
var sh = event.source.getActiveSheet();
var rng = event.source.getActiveRange();
if (sh.getName() == 'Score Input'){
var addresses = ["first","second","third"];
var values = addresses
.map(ad => myGetRangeByName(ad))
.join().split(",");
var item = values.indexOf(rng.getA1Notation());
if (item < addresses.length - 1){
sh.setActiveSelection(addresses[item + 1]); // except last one
}
}
}
function myGetRangeByName(n) {
return SpreadsheetApp.getActiveSpreadsheet().getRangeByName(n).getA1Notation();
}
reference
class NamedRange

How to log data into the last row of the specified column instead of all columns?

Currently, my script is logging values in E based on the position of the last input in columns A,B. Is there a way to prevent these gaps?
var sss = SpreadsheetApp.openById('sampleID');
var ss = sss.getSheetByName('Forecast data');
var range = ss.getRange('B126');
const now = new Date();
const data = range.getValues().map(row => row.concat(now));
var tss = SpreadsheetApp.openById('sampleID2');
var ts = tss.getSheetByName('Archived Data');
ts.getRange(ts.getLastRow()+1, 5,1,2).setValues(data);
}
Try something like this:
ts.getRange(getLastRow_(ts, 5) + 1, 5, 1, 2).setValues(data);
Here's a copy of the getLastRow_() function:
/**
* Gets the position of the last row that has visible content in a column of the sheet.
* When column is undefined, returns the last row that has visible content in any column.
*
* #param {Sheet} sheet A sheet in a spreadsheet.
* #param {Number} columnNumber Optional. The 1-indexed position of a column in the sheet.
* #return {Number} The 1-indexed row number of the last row that has visible content.
*/
function getLastRow_(sheet, columnNumber) {
// version 1.5, written by --Hyde, 4 April 2021
const values = (
columnNumber
? sheet.getRange(1, columnNumber, sheet.getLastRow() || 1, 1)
: sheet.getDataRange()
).getDisplayValues();
let row = values.length - 1;
while (row && !values[row].join('')) row--;
return row + 1;
}
An alternative way to find it is via filter().
Code:
// Sample data to be iserted
data = [[2.4, '5/5/2021']]
var tss = SpreadsheetApp.openById(sampleID2);
var ts = tss.getSheetByName('Archived Data');
// get values on column E and filter the cells with values and get their length
var column = ts.getRange("E1:E").getValues();
var lastRow = column.filter(String).length;
ts.getRange(lastRow + 1, 5, 1, 2).setValues(data);
Sample data:
Output:
Note:
This approach is good when column has no blank cells in between. When you skip a cell, it will not calculate the lastRow properly and might overwrite data. But as long as you do not have gaps in your column, then this will be good.
Resource:
Determining the last row in a single column

getRange variable range

I am trying to use a google sheet to rank a list of elements. This list is continually updated, so it can be troublesome to update the list if i already have hundreds of elements ranked and need to rank 10 new ones. Rather than having to re-rank some of the previously ranked elements every time (whether manually or using formulas), i thought it easier to write a macro that would re-rank for me.
1 - element A
2 - element B
3 - element C
new element: element D
For instance if i wanted element D to be ranked 2nd, i would need to change element B to 3 and element C to 4. This is tedious when doing hundreds of elements.
Here is my code so far but I get stuck with the getRange lines. Rankings are in column A.
function RankElements() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = s.getActiveCell();
var v1 = r.getValue();
var v2 = v1 + 1
var v3 = v2 + 1
var lastRow = s.getLastRow();
s.getRange(1,v2).setValue(v2);
s.getRange(1,v3).autoFill(s.getRange(1,v3+":"+1,lastRow), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
s.getRange(1,v3+":"+1,lastRow).copyTo(s.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
s.getFilter().sort(1, true);
};
You can do the following:
Iterate through all values in column A.
For each value, check if (1) ranking is equal or below the new one, and (2) it's not the element that is being added.
If both these conditions are met, add 1 to the current ranking.
It could be something like this:
function RankElements() {
const sheet = SpreadsheetApp.getActiveSheet();
const cell = sheet.getActiveCell();
const row = cell.getRow();
const newRanking = sheet.getActiveCell().getValue();
const firstRow = 2;
const columnA = sheet.getRange(firstRow, 1, sheet.getLastRow() - 1).getValues()
.map(row => row[0]); // Retrieve column A values
for (let i = 0; i < columnA.length; i++) { // Iterate through column A values
if (columnA[i] >= newRanking && (i + firstRow) != row) {
sheet.getRange(firstRow + i, 1).setValue(columnA[i] + 1); // Add 1 to ranking
}
}
sheet.getFilter().sort(1, true);
};

Sequence number for each different value in a colum

I have an Input column with a sequence of two different letters. As result I want to get something like on the picture. This formulas I will use with ARRAYFORMULA to get unlimited count of rows. To get BLOCK № I was trying to use =COUNTIFS($B$2:B2,"N") but it works only if I copy the formula manually down the column, but if I do:
=ARRAYFORMULA(COUNTIFS(($B$2):(B2:B),"N"))
It doesn't work.
How can I replicate the behavior of this function without needed to manually copy it?
I'd recommend writing a script to fill the Block Nos.
I'll assume the topmost letter begins at cell input!A4 and you want the Block Nos from cell input!C5 and below. Go to the menu bar of the spreadsheet and select Script Editor. Then write the following scripts:
//the main function
function writeBlocks() {
var sheet = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName('input');
var numRows = sheet.getLastRow();
var startRow = 4;
var inputCol = 1;
var outputCol = 3;
var block = 0;
//clear old Block Nos
sheet.getRange(startRow, outputCol, numRows - 3, 1)
.clearContent();
//recalculate LastRow in case there are fewer new inputs than old outputs
numRows = sheet.getLastRow();
//get input data
var input = sheet.getRange(startRow, inputCol, numRows - 3, 1)
.getValues;
//write output data
for (var i = 0; i < input.length; i++) {
block += input[i] == "N" ? 1 : 0;
sheet.getRange(startRow + i, outputCol)
.setValue(block);
}
}
//create new menu
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push({name: "Calculate blocks", functionName: "writeBlocks"});
ss.addMenu("Custom functions", menuEntries);
}
Save it all, refresh the spreadsheet, and there should be a new option on the menu bar. When you select that option, it will clear the old Block Nos and generate new ones based on the current inputs. Hope this helps.

Combine two Google spreadsheet together and keep it sync

I have two sheet from others and wish to create another one which combine both sheet together.
e.g.
SheetA:
Name value
A 10
B 20
SheetB:
Name value
C 30
D 40
Then I want to create one SheetC:
Name value
A 10
B 20
C 30
D 40
And if I change SheetC, I wish data synced to SheetA or SheetB automatically.
I can implement the doc combine with below script:
function runme() {
var docA = "<Doc Id A>";
var docB = "<Doc Id B>";
var sheetName = "Sheet1";
appendSheet(docA,sheetName);
appendSheet(docB,sheetName);
}
function appendSheet(docId,sheetName) {
var sourceSpread = SpreadsheetApp.openById(docId);
var sourceSheet = sourceSpread.getSheetByName(sheetName)
var activeSpread = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = activeSpread.getActiveSheet();
var activeSheetName = activeSheet.getSheetName();
sourceRng = sourceSheet.getDataRange();
sourceRows = sourceRng.getValues(),
activeSheet.appendRow(sourceRows[0]);
for (i = 1; i < sourceRows.length; i += 1) {
activeSheet.appendRow(sourceRows[i]);
}
Logger.log("total: " + i);
}
But it seems it's difficult to sync information from docC to docA and docB.

Resources