Sum if in multiple sheets in Google Sheets - google-apps-script

I am looking for funcion/code for Google Sheet document which would enable me to sum hours (numbers) on multiple sheets based on names.
Basically I need this to create a prediction tool for project hours allocation to control potential overbooking of hours.
Here's link to the file:
https://docs.google.com/spreadsheets/d/1p-MGD1E7uj7_wkGoDPDH_Qgc-jRKJH55d2TvMG9VsBk/edit?usp=sharing
Sheets will be regurarly updated.
I've tried to use INDIRECT funcion but I see that doesn't work in this case.

Try with this function: I've modified the location of your sheets' names since it would be overlapped if you had more names. With MAKEARRAY it creates the chart counting the names, and with REDUCE it sums the values of each sheet you have in your range. Adapt the ranges accordingly to your source sheet!
=MAKEARRAY(COUNTA(A6:A),COUNTA(B5:5),LAMBDA(r,c,
REDUCE(,A2:B4,LAMBDA(a,b,a+ IFERROR(INDEX(INDIRECT("'"&b&"'!B4:M"), MATCH(INDEX(A6:A,r),INDIRECT("'"&b&"'!A4:A"),0), MATCH(INDEX(B5:5,,c),INDIRECT("'"&b&"'!B3:3"),0)))))))
NOTE: Be aware of having matching names in your cells with your actual sheets. You had 'Project 1' instead of 'Project1'

Related

"Currentregion" concept in Google Sheets script language

From inside Google Sheets Script, I am trying to name a variable-sized matrix on the worksheet. In EXCEL VBA, I would go to the top left most cell and select the whole matrix using activecell.currentregion.select.
This would select the whole matrix (e.g. D5:L50) on the worksheet, which I could then name.
Is there the same ability in Google Sheets script language. If not, can anyone figure out how to do this?
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('D5:L50').activate();
you can use macro under:
Tools > Macros > Record macro > and then make your selection > save macro > edit macro
SpreadsheetApp.Range
getDataRegion():
Returns a copy of the range expanded in the four cardinal Directions to cover all
adjacent cells with data in them. If the range is surrounded by empty cells not including those
along the diagonals, the range itself is returned. This is similar to selecting the range and
typing Ctrl+A in the editor.
The question is: how do you refer to a range which contains values and most importantly, the range may change as user add or remove contents.
In Excel VBA, this is dealt with a built-in function called "currentregion". I'm also curious if there is a equivalent in GS.

Connecting Sheets to BigQuery and run the ML PREDICT function

looking for a way to connect a Biqquery model to google sheets and run the sql to predict based on the inputs on the google sheet. The model already exists and the independent variables exist on the sheet(Range of cells) and output has to be pasted in the column beside these input columns.
https://developers.google.com/apps-script/advanced/bigquery#run_query
Looked at the sample code - But this is not dynamic . Looking for something which is dynamic where i can select the range of inputs from the google sheet and paste the output on the same sheet on a column next to input columns.
SELECT predicted_BPrices FROM
ML.PREDICT(MODEL `XXXX',
(this is the range of values it should consider from the sheet)
Basically want to execute the ML.PREDICT function from the Sheets where we pass the independent variables from the sheet and should derive the dependent variable and paste on to the sheets.

How to use a formula written as a string in another cell [evaluate for Google Spreadsheet] [duplicate]

This question already has answers here:
Is there a way to evaluate a formula that is stored in a cell?
(13 answers)
Closed last month.
I read several old posts about Google Spreadsheet missing the evaluate function.
There is any solution in 2016?
The easiest example.
'A1' contains the following string: UNIQUE(C1:C5)
'B1' I want to evaluate in it the unique formula written in 'A1'.
I've tried concatenating in this way: 'B1' containing ="="&A1 but the outcome is the string =UNIQUE(C1:C5).
I've also tried the indirect formula.
Any suggestion to break last hopes, please?
Additional note
The aim is to write formulas in a spreadsheet and use these formulas by several other spreadsheets. Therefore, any change has to be done in one place.
Short answer
Use a script that includes something like var formula = origin.getValue() to get the string and something like destination.setFormula(formula) to return the formula.
Explanation
As was already mentioned by the OP, Google Sheets doesn't have a EVALUATE() built-in function. A custom function can't be used because custom functions can only return one or multiple values but can't modify other cell properties.
A script triggered by a custom menu, events or from the Google Apps Script editor could be used to update the formulas of the specified cells.
Since the formulas will be kept as strings, it could be more easy to keep them in the script rather than in the spreadsheet itself.
Example
The following is a very simple script that adds the specified formula to the active range.
function addFormula() {
var formula = '=UNIQUE(C1:C5)';
var range = SpreadsheetApp.getActiveRange();
range.setFormula(formula);
}
I have a solution for my own use case. My investment broker exports data to its users in (badly-formatted) Excel. I do my own analysis in Google Sheets. I have found copy/pasting entire sheets of data to be accident-prone.
I have partially automated updating each tab of the records. In the sheet where I maintain all the records, the First tab is named "Summary"
Save the broker's .xlsx data to Google Sheets (File | Save as Google Sheets);
In the tab named Summary, enter into a cell, say "Summary!A1" the URL of this Google Sheet;
In cell A2 enter: =Char(34)&","&CHAR(34)&"Balances!A1:L5"&Char(34)&")"
In the next tab, enter in cell A1: ="IMPORTRANGE("&Char(34)&Summary!A1&Summary!A2
The leading double quote ensures that the entry is saved as a text string.
Select and copy this text string
in cell A3, type an initial "=" + Paste Special.
This will produce an importrange of the desired text, starting at cell A3

Google spreadhseet EVAL function

I have a google spreadsheet with different sheets, each one representing a different week.
For example:
1/12 - 1/16
1/19 - 1/23
I want to do a chart based on the content of those sheets. Is there any way I can make a formula and extract the name of the sheet from a content of a cell?
For example something like "=EVAL(A1)!$B$4", then I would have the content from "1/12 - 1/16"!$B$4 instead of having to go through each one of the weeks of the year manually.
Thanks for the help!
There’s no need to use AppScript, INDIRECT is enough to read a sheet name from a cell:
=INDIRECT(A1 & "!$B$4")
However, it looks like Andy’s answer is the way to go if you want to get the sheet name from its index rather than from a cell.
It'd be best to use AppScript. In Tools -> Script Editor make a new AppScript script:
function getSheetName(i) {
var s = SpreadsheetApp.getActiveSpreadsheet().getSheets()
return s[i].getName();
}
With that in your script, you can then use the custom function =getSheetName(<SHEETNUMBER>) and it will retrieve the sheet name based what sheet number it is (starting from 0). From there, just incorporate it into your formulas. You may need to use INDIRECT.
Example: =INDIRECT(getSheetName(1)&"!A1") to get cell A1 in the second sheet.

Workaround for not losing cell's reference because of user doing sorting, while script checks cells value

I have been struggling with the following situation for almost the whole last week and it would be awesome if someone could give me some hint.
The situation:
1. Script finds a particular value in, lets say, 'Sheet1', and gets the row number of the cell containing this value.
2. Since script has found this value, it executes a bunch of actions like creating new spreadsheet and copying numerous 'Sheet1' from dozen other spreadsheets to this newly created spreadsheet, and comparing/ analyzing data.
The problem.
- While script is doing a bunch of other actions, user is able to rearrange cells by, for example, sorting A to Z, which changes the address of previously found cell.
Here is the code that I used in order to verify this:
function WhatHappensIfUserSorts () {
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var rng = sh.getRange("B17"); //Lets say that script finds this cell according to some rules
Utilities.sleep(10000);
rng.setValue("Test Value");
}
Question:
Might there be any workaround for this?
My current ideas.
1. I was thinking about hiding the filter row in the beginning of the script, but this doesn't help a lot, because users can insert new row in the which will change the addresses of the rows below.
The background.
I am trying to create two way synchronization, meaning, each project member has his/ her own spreadsheet with 'Project X', 'Project Y' etc. sheets and no matter who updates their project sheets, all other users that work on the same project get these updates in their project sheets. These updates that have to be tracked are not just the cell values, these are cell notes as well. And this is the reason why script has to do the bunch of other actions, since CopyTo method does not work between spreadsheets.
During my research I found sheetSpider project, but it seems somewhat different and too complicated from what I need.
A simple suggestion would be to give each row a unique identifier so that you could use it to evaluate the target range again before you write back to the sheet.
get target row's unique ID --> do work --> locate target rows ID and use to determine write range --> write back to sheet.
Alternatively, during the operation you could delete the target row and then use appendRow() to drop the updated version back in.
A third and final suggestion might be to temporarily suspend the permissions for the sheet. See: https://developers.google.com/apps-script/reference/spreadsheet/page-protection#setProtected