How to pass cell value to a SUM function - google-sheets

I am trying to do a SUM of last 12 rows in the column (I'll be adding more rows into this column so I wanted to automate the calculation).
First of all, I am able to get the value of last cell with some value in this column by =SUMPRODUCT(MAX((B1:B200<>"")*ROW(B1:B200))) - result is stored in C1. However, I am not sure how to use this value inside the SUM formula, I was thinking something like =SUM(B(get value of C1)-12:B(get value of C1).
I tried multiple things but none of them have worked - I also don't mind using a different approach if it gets the job done.

You can create your own custom function to do that using Google Apps Script (GAS).
Try the following:
function onEdit(e){
var row = e.range.getRow();
var col = e.range.getColumn();
if ( col==2 && e.source.getActiveSheet().getName() == "Sheet1" ){
e.source.getActiveSheet().getRange("C1").setValue(sumLast12());
}
}
function sumLast12() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
var sheet_size = sheet.getLastRow();
var elmt = sheet.getRange("B1:B"+sheet_size).getValues().flat([1]);
var elmt12 = elmt.slice(-12);
var sum = 0;
for( var i = 0; i < elmt12.length; i++ ){
sum += parseInt( elmt12[i], 10 );
}
return sum;
}
Explanation:
In order to activate this functionality go the menu bar on top of the
spreadsheet file and click on Tools => Script editor and copy the
aforementioned code into a blank script document (see attached
screenshot for more information) and save the document (cntrl+s).
After the script has been saved, everytime you edit a cell in column
B (either by adding a new value on the bottom or modify an existing value, the script will automatically update the value in
cell C1 with the sum of the last 12 values in column B.
Note that if you don't want to change my code, name the sheet you are working with as Sheet1.

Does this work?
=SUM(FILTER(B:B,ROW(B:B)>=MAX(ROW(B:B))-12)

Related

Add checkbox to lock cells with dropdown lists in Google Sheets

Suppose I have a Google Sheet where columns A:G are dropdown lists.
I would like to add an option to lock choices in columns A:G using a checkbox in column H, where checking the box in this column will lock columns A:G so they can not be edited (unless the checkbox in column H is de-selected).
Is it possible to do this on Google Sheets?
I have created the following script that is based on a previous script I found in a response here in StackOverflow a long time ago.
function onEdit(e)
{
var editRange = {
top : 1,
bottom : 1000,
left : 2,
right : 2
};
var cell = e.range;
// Exit if we're out of range
var thisRow = cell.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
var thisCol = cell.getColumn();
if (thisCol < editRange.left || thisCol > editRange.right) return;
var val1 = e.range.getRow();
var val2 = e.range.getColumn();
var validation = SpreadsheetApp.getActive().getSheetByName("Testing Sheet").getRange(val1, val2-1);
if(cell.isChecked()){
var rule = SpreadsheetApp.newDataValidation().requireValueInList(['Red', 'Blue']).build();
}
else{
var rule = SpreadsheetApp.newDataValidation().requireValueInList(['Black', 'White']).build();
}
validation.setDataValidation(rule);
}
I modified the range at the beginning so it only recognizes the change in column B, but you can just change it to 8 for column H. Since the script uses an onChange trigger, what you need to validate first is if the edit was made in the range where the checkboxes are, then I set the range of the cell that I was going to change based on the value from the checkbox and created the data validation rule with the expected values depending on the conditional.
In your case you can just add the rest of the ranges for the other columns with more validation variables and change the column parameter to -2, -3, or -4 depending on the cells you will be editing for example, and then create more validation rules according to the data you need in the dropdowns.
Here is an example of how this would work:
References:
onEdit()
Data Validation

Function to check for duplicates based on condition

I have a simple Google sheet that records what sessions people are signed up for (3 concurrent sessions per day):
The same person cannot be in more than 1 session on a given day. I'd like to create a function in column B that checks for that situation and flags it, as in Susan, Keith, and Amy in the example above (I've highlighted in yellow the conditions that would trigger a flag).
If there were just one date, I'd use a countif (or maybe countifs?) to check for more than 1 TRUE for that date. But with multiple dates, I think some sort of iterative function or query is needed. I have a feeling I may be missing a simple formula, but it's eluding me. I may add more dates, so the solution needs to allow for n number of dates in the range.
UPDATE: My scenario has become a little more complex. I'm designating a potential role each person can play in each session and then using the checkboxes to indicate who is playing what role in each session. A given person can't be in more than 1 session per day (but a given person may be in 0 sessions on a given day). The below image shows this updated scenario, with the yellow highlights showing the conditions that I want flagged via the function in column B.
Here's a link to the Google sheet if you want to create a copy.
Given the use case provided, you can apply the formula below to B3 and drag the auto-complete handle:
=IF(ARRAYFORMULA(SUM(INT(C3:K3))) = COUNTUNIQUE($C$1:$1), "", "FLAG")
I'm converting the Boolean values to INT and summing them up. If the sum is equal to the count of unique days in the first row, then everything is fine, otherwise, FLAG!
In other words, if there are more (or less) checks than days, it should be flagged.
You can also set up a conditional formatting to paint the cell accordingly.
Alternatively, if you’d like to treat each scenario you can use =IFS() as below:
=IFS(ARRAYFORMULA(SUM(INT(C3:K3))) > COUNTUNIQUE($C$1:$1), "HIGHER", ARRAYFORMULA(SUM(INT(C3:K3))) < COUNTUNIQUE($C$1:$1), "LOWER", ARRAYFORMULA(SUM(INT(C3:K3))) = COUNTUNIQUE($C$1:$1), "OK")
References:
Sheets Functions documentation
IF
IFS
ARRAYFORMULA
SUM
INT
COUNTUNIQUE
EDIT:
Since the changes in the original scope significantly impacted my previous answer, here is a suggestion using a custom formula:
function checkFlags(){
var ss = SpreadsheetApp.getActive(); // get active Sheets
var ws = ss.getSheetByName("Sheet1"); // getting tab named "Sheet1"
var currentCellRange = ss.getActiveRange(); // getting active cell, in the context of a custom formula, it gets the one being calculated at the time
var rowIndex = currentCellRange.getRowIndex(); //getting current row number
var rowValues = ws.getRange(`${rowIndex}:${rowIndex}`).getValues()[0]; //getting row cells values
var sessionsList = []; //temp variable to store useful data from cells
for (var i = 0; i < rowValues.length; i++) { //reading cells on the row to create a date/flag array
var cell = rowValues[i]; //getting Range of current cell
if (typeof(cell) == 'boolean'){ //if the current cell has a boolean value, it is a session flag
var headerDate = ws.getRange(2, (i+2)).getDisplayValue(); //getting the header value on row 2 (current date for the session flag)
sessionsList.push({date: headerDate, session: cell});//storing date and session flag value on the temp variable
}
};
var groupBy = function(xs, key) { //handle function to proccess the sessionsList variable and group flag values by 'date'
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
var tempGroupedArray = groupBy(sessionsList, 'date'); //grouping temp sessionsList by 'date'. This will return an array like [ { date: '<date>', session: true/false }, { date: '<date>', session: true/false }, ...]
for (dateFlags of Object.entries(tempGroupedArray)) {//looping through the `grouped by date` array
var tempCount = dateFlags[1].filter(x => x.session==true).length; //how many `trues` are for this date
if (tempCount > 1){ //if there is more than one session set as "true" for this date...
return 'FLAG'; //...immediately stop execution and return FLAG
}
};
//if it passed the loop above, it means there is no flags
return ''; //then return blank
}
NOTES: This custom formula will not update the result in the cell after a change on the flag values, you must delete/paste it to force if wanted.

Problem with recording data change in google sheet

I have two sheets. On one sheet (Sheet1), I have a column 'A' which has 5 fixed values, and a column 'B' which records the timestamp value in 'A' change. The other sheet (Sheet2) is meant to record all the changes in the first sheet in one day.
I use this simple query in the recording sheet:
=QUERY(Sheet1!A$1:X,"select * where C>="& Sheet2!D1)
with Sheet1!C has timestamps and Sheet2!D1 is the timestamp of 12:00 AM today
The problem is when I change the value of a row in C columns more than one time, instead of creating a new row in Sheet2 it change the value of that row in Sheet2 into new values.
So how do I change my code to get my desire results?
EDIT 2: here is my new code, but it doesn't help.
function importdata(x) {
// var row = x.range.getRow();
// var col = x.range.getColumn();
var addedsheet=SpreadsheetApp.getActive().getSheetByName("Change"); // Sheet where I want to keep the record of the change
var original_sheet = SpreadsheetApp.getActive().getSheetByName("Master"); //sheet where the change is happended
var compared_value = addedsheet.getRange(1,4).getValue(); // Cell D1 of sheet "Change", which has timestamp of today
var insert_area = original_sheet.getRange("A2:X").getValues() // area to get value from "Master" sheet to put into "Change"
var compared_area = original_sheet.getRange("C2:C").getValues(); // area where has timestamp
if (compared_area >= compared_value){
addedsheet.values.append([insert_area])}
} //if timestamp of one row from Master is greater than the value at Change!D1 =>append that row at the end (this is what I'm trying to do)
EDIT 3: I fixed the above code by append[insert_area][0] instead of [insert_area]
But then I have a new problem: there will a chance that a row in sheet 1 will be overwrited in sheet 2. I try something like this, but it returns nothing on the sheet.
function for_each_row(){
var addedsheet=SpreadsheetApp.getActive().getSheetByName("Change"); // Sheet where I want to keep the record of the change
var original_sheet = SpreadsheetApp.getActive().getSheetByName("Master"); //sheet where the change is happended
var compared_value = addedsheet.getRange(1,4).getValue(); // Cell D1 of sheet "Change", which has timestamp of today
var number_of_row_2 = addedsheet.getLastRow;
var number_of_row_1 = original_sheet.getLastRow();
for (var i=2; i<number_of_row_1 +1; i++){
var compared_stamp = original_sheet.getRange("C"+i).getValues();
var insert_values = (original_sheet.getRange(i,1,1,24).getValues())
if (compared_stamp > compared_value){
var insert_values = (original_sheet.getRange(i,1,1,24).getValues());
for (var j = 2; j<number_of_row_2 +1; j++){
var value_from_sheet = addedsheet.getRange(j,1,1,24).getValues();
if (insert_values ===value_from_sheet){
return
}
else(
addedsheet.appendRow(insert_values[0]))
}
}
}
}
My thought is if a row satisfies the 1st condition then the value will be check in sheet 2. If sheet 2 didn't have that row then append that row.
Issue:
If I understand you correctly, you want to do the following:
If sheet Master is edited, iterate through all rows in this sheet (excluding headers), and for each row, check the following:
Column C has a higher value than cell D1 in sheet Change.
This row, with these exact values, does not exist in Change.
If these conditions are meet, append the row to Change.
Solution:
Use filter and some to filter out rows that don't match your two conditions, and use setValues to write the resulting rows to your other sheet.
Code snippet:
function onEdit(e) {
var editedSheet = e ? e.range.getSheet() : SpreadsheetApp.getActiveSheet();
if (editedSheet.getName() === "Master") {
var addedSheet = SpreadsheetApp.getActive().getSheetByName("Change");
var compared_value = addedSheet.getRange(1,4).getValue();
var newData = editedSheet.getRange("A2:X" + editedSheet.getLastRow()).getValues();
var currentData = addedSheet.getRange("A2:X" + addedSheet.getLastRow()).getValues();
var filteredData = newData.filter(row => row[2] >= compared_value)
.filter(row => !currentData.some(currentRow => JSON.stringify(currentRow) === JSON.stringify(row)));
addedSheet.getRange(addedSheet.getLastRow()+1,1,filteredData.length,filteredData[0].length).setValues(filteredData);
}
}
Note:
An easy way to check if two rows have the same values is using JSON.stringify(row).
I'm assuming the timestamps are not Dates. If they are, you should compare them using getTime(). So you should change the corresponding code to newData.filter(row => row[2].getTime() >= compared_value.getTime()).

Formula to ignore hidden columns

I am trying to count visible columns in a spreadsheet with no luck. I've been trying using SUBTOTAL function but it's applied to hidden/visible rows only. I also tried working with CELL("width") function, but it doesn't return 0 when a cell is hidden
Is there any other option to ignore hidden columns in a count formula?
You can definitely create your own custom function using Google Apps Script.
For example, the following function counts the number of visible columns in your active sheet:
function countVisibleColumns() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet()
var n_cols = sheet.getMaxColumns();
var hidden_cols = []
var cnt = 0;
for (var i=1; i<=n_cols ; i++) {
if ( sheet.isColumnHiddenByUser(i) ){
continue;}
else {cnt +=1} }
Logger.log(cnt)
return cnt;
}
You just need to click on Tools => Script editor and then copy the aforementioned code into a blank script. Then you can directly use the function as a formula in the google sheet like =countVisibleColumns().
See screenshot attached for more information.

Find duplicates of two column combination

Let I have two column, A and B . How I can find duplicates of this two column combination?
As shown in the picture, duplicate of combination of two column will be colored.
You can get the duplicates highlighted, with the help of following script:
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{name : "Check Duplicates",functionName : "duplicates"}];
sheet.addMenu("Scripts", entries);
};
function duplicates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Sheet1');
var r = s.getRange("A:B");
var v = r.getValues();
r.setBackground('white');
var f = r.getBackgrounds();
var lastrow = getLastPopulatedRow(v);
for( var i=0;i<lastrow;i++){
for( var j=i+1;j<lastrow;j++){
if( ( v[i][0]==v[j][0] && v[i][1]==v[j][1] ) || ( v[i][0]==v[j][1] && v[i][1]==v[j][0] ) ) {
f[i][0]='lightgreen';
f[i][1]='lightgreen';
f[j][0]='lightgreen';
f[j][1]='lightgreen';
}
}
}
r.setBackgrounds(f);
};
function getLastPopulatedRow(data) {
for (var i=data.length-1;i>=0;i--)
for (var j=0;j<data[0].length;j++)
if (data[i][j]) return i+1;
return 0;
};
Run the function "duplicates" from the script editor. You will also be able to run it from the custom menu "Scripts" from your spreadsheet.
Here is the Screenshot
I think #jwilson gave the proper answer.
However, here is a shortcut method, it may help you.
Select a cell and use CONCATENATE function to join two column value with a space between them. Then you can search the current column if they have duplicates.
For example, using your example image, select your column C1 and add =CONCATENATE(A1," ",B1) and drag this to applicable the formula for all cells in column C. Then add a conditional format rules for column C to check duplicates using =countif(C:C,C1)>1.
Here what is happening is, value of column A and B is concatenating first, then compared if those have duplicate entry in the record.
Use the following Custom formula for Conditional formatting:
=countif(arrayformula($A:$A&"|"&$B:$B);$A1&"|"&$B1)>1

Resources