Lock Google Sheets cells/range after condition is met - google-apps-script

We're using Google Sheets for financial reconciliation internally and facing some mistakes in it.
There is a spreadsheet with all the data to which almost everyone in company has access for editing.
What I want to do is to lock certain cells for all users except few people when simple condition (for example, cell fill color is changed to red) is met.
So the function description looks like:
everyone has access to spreadsheet
cells in range (which one should be locked) are not locked
cells are not locked until condition is met
user enters value to cell/range
user applies condition (fill color, for example)
cell locks. access from all users except few ones is removed
users with access could edit/unlock
It would be much appreciated if someone could help with the exact function to apply.
Many thanks in advance!
The only thing I did found is the documentation which is close to my problem: https://developers.google.com/apps-script/reference/spreadsheet/range
https://developers.google.com/apps-script/reference/spreadsheet/protection
But I'm zero in Apps Script which is used by Google Sheets(

This gets you close but unfortunately I ran into the problem of onEdit events do not consider background color changes apparently... So ultimately this will only fire after a cell value changes.
If the cell is Red and the range is not yet protected, it will be protected as you being the only editor. If the background is not red it will either strip away the protection or not change.
/**
* Protects and unprotects ranges;
* #param {Object} e event object;
*/
function onEdit(e) {
//access cell formats;
var bgColor = e.range.getBackground();
var bold = e.range.getFontWeight();
//access edited range, value and sheet;
var rng = e.range;
var val = e.value;
var sh = rng.getSheet();
//access edited range row and column;
var row = rng.getRow();
var col = rng.getColumn();
//access protections;
var ps = sh.getProtections(SpreadsheetApp.ProtectionType.RANGE);
//filter out other cells protections;
ps = ps.filter(function(p){
var ptd = p.getRange();
if(row===ptd.getRow()&&col===ptd.getColumn()) {
return p;
}
})[0];
//SpreadsheetApp.getActive().toast(bgColor); //Uncomment to get a toast displaying background color of edited cell.
//if protection not set -> protect;
if(!ps) {
if (bgColor === '#ff0000' && bold === 'bold') {
SpreadsheetApp.getActive().toast("Cell Locked");
var protection = rng.protect(); //protect Range;
var users = protection.getEditors(); //get current editors;
var emails = [Session.getEffectiveUser(),'email1#email.com','email2#email.com']; //declare list of users (emails)
protection.addEditor(Session.getEffectiveUser());
protection.addEditors(emails);
protection.removeEditors(users); //remove other editors' access;
}}else {
if(!val || bgColor != '#ff0000' || bold != 'bold') { ps.remove(); } //if cell is empty -> remove protection;
}
}
Perhaps someone can improve on this and adjust for background events only. Also if working fast this doesn't keep up well.

Related

Show/Hide Columns Using A Checkbox

I am a wanna-be developer who is trying to figure out how to hide a set of columns based off a checkbox being clicked.
Would anyone want to help with this code?
I have 12 different sheets(one for each month) and I would like to hide columns A-H with the checkbox in I being clicked.
Ideally I can implement on each individual sheet.
Link to spreadsheet
There are few ways one can do it.
Easiest and most recommended among all is to group those column and it will have pretty much same use which you're looking for.
If you're willing to use appscript for it. Here how it should be done:-
Open Script Editor from your spreadsheet.
Declare the onEdit simple trigger which will run every time when sheet will be edited.
So whenever you'll click on tickbox on I1 this function will trigger.
When a trigger fires, Apps Script passes the function an event object as an argument, typically called e.
For this object, we're gonna have the information we need to do our task, and also to restrict our operation to only to those months sheet and range belongs to it.
Here is the code, I tried my best to explain what happening in the code:-
function onEdit(e)
{
var rangeEdited = e.range; // This will us range which is edited
var sheetEdited = rangeEdited.getSheet().getName() // from range we can get the sheetName which is edited
var mySheets = ["Jan List","Feb List"] // Put all the month sheet name in this array where you want to have this functionality
var rowEdited = rangeEdited.getRow() // From Range we can get Row which is edited
var columnEdited = rangeEdited.getColumn() // From Range we can get Column which is edited
if(mySheets.indexOf(sheetEdited) > -1) // Now we want to only restrict the operation on those sheets,so if other sheet is edited, we shouldn't run our hide function
{
if(rowEdited === 1 && columnEdited === 9) // we're further restricting the range of operation to run this function when only I1 is edited that is Row:- 1 and Col:- 9
{
hideUnhide(sheetEdited) // calling our hide function within OnEdit and passing sheetName as an argument in it
}
}
}
function hideUnhide(sheetEdited) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssname = ss.getSheetByName(sheetEdited) // accessing the sheet which is edited
var isItHidden = ssname.isColumnHiddenByUser(1) // checking if range is already hidden
if(isItHidden === false) // if No, hide that range
{
ssname.hideColumns(1, 6)
}
else // if Yes, unhide that range
{
var hideThisRange = ssname.getRange('A:H')
ssname.unhideColumn(hideThisRange)
}
}
Documentation:-
AppScript Events

Apps Script to update Timestamp when data is inserted automatically in google sheet

This code works fine when data is edited in Column 3 or being copy-pasted but if the cursor remains at column 1 at the time of the whole row being copy/pasted, it won't update and secondly, if salesforce sends data to column 3, it doesn't work that time too, please help me here.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var sName = s.getName();
var r = s.getActiveCell();
var row = r.getRow();
var ar = s.getActiveRange();
var arRows = ar.getNumRows()
// Logger.log("DEBUG: the active range = "+ar.getA1Notation()+", the number of rows = "+ar.getNumRows());
if( r.getColumn() == 3 && sName == 'Sheet1') { //which column to watch on which sheet
// loop through the number of rows
for (var i = 0;i<arRows;i++){
var rowstamp = row+i;
SpreadsheetApp.getActiveSheet().getRange('F' + rowstamp.toString()).setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm"); //which column to put timestamp in
}
}
}//setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm:ss");
Explanation:
Three important things to know:
As it is also stated in the official documentation, the onEdit triggers are triggered upon user edits. This function won't be triggered by formula nor another script. If salesforce or any other service except for the user, edits column C the onEdit trigger is not going to be activated. Workarounds exist, but these workarounds depend on the context of your specific problem. I would advice you to search or ask a specific question about it.
Regarding the other issue you have, you should get rid of active ranges and take advantage of the event object. This object contains information regarding the edit/edits user made.
As it is recommended by the Best Practices you should not set values in the sheet iteratively but you can to that in one go by selecting a range of cells and set the values. In your case, you want to set the same value in all of the cells in the desired range, hence setValue is used instead of setValues. But the idea is to get rid of the for loop.
Solution:
function onEdit(e) {
var s = e.source.getActiveSheet();
var sName = s.getName();
var ar = e.range;
var row = ar.getRow();
var arRows = ar.getNumRows()
if( ar.getColumn() == 3 && sName == 'Sheet1') {
s.getRange(row,6,arRows).setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm");
}
}
Note:
Again, onEdit is a trigger function. You are not supposed to execute it manually and if you do so you will actually get errors (because of the use of the event object). All you have to do is to save this code snippet to the script editor and then it will be triggered automatically upon edits.

Hide Sheets Based on a Cell Value

I'm pretty new to learning app script and looked over/tried to edit this script, but I'm not getting my desired result. I have a sheet titled "Menu" where I'm wanting a user to select from three different drop down options in Cell A2 (e.g. Blue, Yellow, Green). I then want to hide the different sheets based on the selection. So if a user selects "Blue" I want only the sheets that start with the word "Blue" to be visible + the "Menu" sheet and the rest to be hidden. Same for Yellow and Green. As a note there are 13 sheets for each color.
Any help with this is much appreciated.
This is an alternative implementation of #JSmith's answer, using the Sheets REST API to more efficiently hide & unhide a large number of sheets.
To use the Sheets REST API from Apps Script, you will first need to enable it, as it is an "advanced service."
The Sheets API approach enables you to work with the JavaScript representation of the data, rather than needing to interact with the Spreadsheet Service repeatedly (e.g. to check each sheet's name). Additionally, a batch API call is processed as one operation, so all visibility changes are reflected simultaneously, while the Spreadsheet Service's showSheet() and hideSheet() methods flush to the browser after each invocation.
var MENUSHEET = "Menu";
function onEdit(e) {
if (!e) return; // No running this from the Script Editor.
const edited = e.range,
sheet = edited.getSheet();
if (sheet.getName() === MENUSHEET && edited.getA1Notation() === "A2")
hideUnselected_(e.source, e.value);
}
function hideUnselected_(wb, choice) {
// Get all the sheets' gridids, titles, and hidden state:
const initial = Sheets.Spreadsheets.get(wb.getId(), {
fields: "sheets(properties(hidden,sheetId,title)),spreadsheetId"
});
// Prefixing the choice with `^` ensures "Red" will match "Reddish Balloons" but not "Sacred Texts"
const pattern = new RegExp("^" + choice, "i");
// Construct the batch request.
const rqs = [];
initial.sheets.forEach(function (s) {
// s is a simple object, not an object of type `Sheet` with class methods
// Create the basic request for this sheet, e.g. what to modify and which sheet we are referencing.
var rq = { fields: "hidden", properties: {sheetId: s.properties.sheetId} };
// The menu sheet and any sheet name that matches the pattern should be visible
if (s.properties.title === MENUSHEET || pattern.test(s.properties.title))
rq.properties.hidden = false;
else
rq.properties.hidden = true;
// Only send the request if it would do something.
if ((!!s.properties.hidden) !== (!!rq.properties.hidden))
rqs.push( { updateSheetProperties: rq } );
});
if (rqs.length) {
// Visibility changes will fail if they would hide the last visible sheet, even if a later request in the batch
// would make one visible. Thus, sort the requests such that unhiding comes first.
rqs.sort(function (a, b) { return a.updateSheetProperties.properties.hidden - b.updateSheetProperties.properties.hidden; });
Sheets.Spreadsheets.batchUpdate({requests: rqs}, initial.spreadsheetId);
}
}
There are a fair number of resources to be familiar with when working with Google's various REST APIs:
Google APIs Explorer (interactive request testing)
Google Sheets REST API Reference
Partial Responses (aka the "fields" parameter)
Determining method signatures
google-sheets-api
A little testing in a workbook with 54 sheets, in which I used the Sheets API to apply some changes and #JSmith's code to revert the changes, showed the API approach to be about 15x faster, as measured with console.time & console.timeEnd. API changes took from 0.4 to 1.1s (avg 1s), while the Spreadsheet Service method took between 15 and 42s (avg 20s).
try this code:
function onEdit(e)
{
//filter the range
if (e.range.getA1Notation() == "A2")
{
// get value of cell (yellow||green||...)
onlySheet(e.value)
}
}
function onlySheet(str)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
//get all sheets
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++)
{
//get the sheet name
var name = sheets[i].getName();
// check if the sheet is not the "Menu" sheet
if (name != "Menu")
{
// check if the name of the sheet contains the value of the cell, here str
//if it does then show sheet if it doesn't hide sheet
if (name.match(new RegExp(str, "gi")))
sheets[i].showSheet();
else
sheets[i].hideSheet();
}
}
}

Google Sheets move cursor onEdit trigger based on cell content

I am trying to write a Google Sheets Apps Script function that checks the content of the current active cell, matches it to the content of another cell, then moves the cursor according to the result of that check.
For a spreadsheet as this example one:
https://docs.google.com/spreadsheets/d/1kpuVT1ZkK0iOSy_nGNPxvXPTFJrX-0JgNmEev6U--5c/edit#gid=0
I would like the user to go to D2, enter a value followed by Tab, then while the active cell is in E2, the function will check if the value in D2 is the same in B2. If it is, stays in E2.
Then we enter the value in E2 followed by Tab, the function checks if it's the same as C2, if it is, then moves from F2 down and left twice to D3. So if all the values are entered correctly, the cursor zig-zags between the cells in D, E and F as shown below:
The closest I could find is the answer to the one below, but it involves clicking on a method in the menu each time:
Move sheet rows on based on their value in a given column
I imagine the function could be triggered at the beginning of editing the document, then it keeps moving the cursor until the document is completed, at which point the function can be stopped.
Any ideas?
EDIT: what I've tried so far:
I have managed to change the position to a hard-coded position 'D3' and to create a function that moves one down with these functions:
function onOpen() {
var m = SpreadsheetApp.getUi().createMenu('Move');
m.addItem('Move to D3', 'move').addToUi();
m.addItem('Move to one below', 'move2').addToUi();
m.addItem('Move down left', 'move_down_left').addToUi();
}
function move() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var range = s.getRange('D3');
s.setActiveRange(range);
}
function move2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = s.getActiveRange();
var c = r.getCell(1,1);
var target = s.getRange(c.getRow() + 1, c.getColumn());
s.setActiveRange(target);
}
function move_down_left() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = s.getActiveRange();
var c0 = r.getCell(1,1);
var r1 = s.getRange(c0.getRow(), c0.getColumn() - 1);
var c1 = r1.getCell(1,1);
var r2 = s.getRange(c1.getRow(), c1.getColumn() - 2);
var c2 = r2.getCell(1,1);
if (c1.getValue() == c2.getValue()) {
var target = s.getRange(c1.getRow() + 1, c1.getColumn() - 1);
s.setActiveRange(target);
}
}
As I mentioned in my comment, you want to use a simple trigger function (so that it works for all users without requiring them to first authorize the script). There are naturally some limitations of simple triggers, but for the workflow you describe, they do not apply.
A key principle of a function receiving the on edit trigger invocation is that it has an event object with data about the cell(s) that were edited:
authMode:
A value from the ScriptApp.AuthMode enum.
oldValue:
Cell value prior to the edit, if any. Only available if the edited range is a single cell. Will be undefined if the cell had no previous content.
range:
A Range object, representing the cell or range of cells that were edited.
source:
A Spreadsheet object, representing the Google Sheets file to which the script is bound.
triggerUid:
ID of trigger that produced this event (installable triggers only).
user:
A User object, representing the active user, if available (depending on a complex set of security restrictions).
value:
New cell value after the edit. Only available if the edited range is a single cell.
Of these, we will use range and value. I will leave the business case of handling edits to multiple-cell ranges to you. Stack Overflow is, after all, not where you obtain turnkey solutions ;)
function onEdit(e) {
if (!e) throw new Error("You ran this from the script editor");
const edited = e.range;
if (edited.getNumRows() > 1 || edited.getNumColumns() > 1)
return; // multicell edit logic not included.
const sheet = edited.getSheet();
if (sheet.getName() !== "your workflow sheet name")
return;
// If the user edited a specific column, check if the value matches that
// in a different, specific column.
const col = edited.getColumn(),
advanceRightColumn = 5,
rightwardsCheckColumn = 2;
if (col === advanceRightColumn) {
var checkedValue = edited.offset(0, rightwardsCheckColumn - col, 1, 1).getValue();
if (checkedValue == e.value) // Strict equality may fail for numbers due to float vs int
edited.offset(0, 1, 1, 1).activate();
else
edited.activate();
return;
}
const endOfEntryColumn = 8,
endCheckColumn = 3,
startOfEntryColumn = 4;
if (col === endOfEntryColumn) {
var checkedValue = edited.offset(0, endCheckColumn - col, 1, 1).getValue();
if (checkedValue == e.value)
edited.offset(1, startOfEntryColumn - col, 1, 1).activate();
else
edited.activate();
return;
}
}
As you digest the above, you'll note that you are required to supply certain values that are particular to your own workflow, such as a sheet name, and the proper columns. The above can be modified in a fairly straightforward manner to advance rightward if the edited column is one of several columns, using either a constant offset to the respective "check" column, or an array of respectively-ordered offsets / target columns. (Such a modification would almost certainly require the use of Array#indexOf.)
A caveat I note is that strict equality === fails if your edits are numbers representable as integers, because Google Sheets will store the number as a float. Strict equality precludes type conversion by definition, and no int can ever be the exact same as a float. Thus, the generic equality == is used. The above code will not equate a blank check cell and the result of deleting content.
Method references:
Range#offset
Range#activate

How to change color of Cell depending on value change

I have an automatically updating spreadsheet that updates different price values.
How could I set up Conditional Formatting so that if the value in the cell raises, the cell turns green. And if it gets lowered, the cell turns red. And optionally, if the cell doesn't change at all, it stays white.
Any help is appreciated :)
In addition to the recommendations in comment, you should consider using the onEdit() function that runs automatically when a user changes the value of any cell in a spreadsheet. It use the information in the event object to respond appropriately.
Found this related SO question which suggested to use if statements.
Sample code:
function onEdit(event)
{
var ss = event.range.getSheet();
if (ss.getName() !== "Sheet2") return; // Get out quickly
var changedCell = event.source.getActiveRange();
var changedCellA1 = changedCell.getA1Notation();
if (changedCellA1 !== 'B3') return;
var c = event.value; // We know we edited cell B3, just get the value
var background = 'white'; // Assume 1 <= c <= 2
if (c > 2) {
background = 'red';
}
else if (c < 1) {
background = 'black';
}
changedCell.setBackground(background);
ss.getRange("B12").setBackground(background);
}
You can also check these related links:
Google Spreadsheet: Script to Change Row Color when a cell changes text;
Google Spreadsheet: Script to change background color of a cell based on a hex code in the neighboring cell
Hope this helps!