Connecting Sheets to BigQuery and run the ML PREDICT function - google-apps-script

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.

Related

Sum if in multiple sheets in Google Sheets

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'

"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.

Copying and pasting data in Google Sheets to another column

I have a dashboard that is reporting live data based on a growing customer database, and I am currently tracking the daily process by copying and pasting the data in the column into the next on a daily basis.
I'd like to create a script that automatically copies the values from an array (B:B) for example, into another sheet OR into the next available column to the right.
You may refer to the image below for further clarity.
Sample
I saw may threads on this but they were all about copying into a different row, as opposed to a different column.
Many thanks in advance!
The getLastRow() method is used to return the position of the last row that has content.
The getLastColumn() method is used to return the position of the last column that has content.
Therefore, in order to be able to copy the data into the next available column, I suggest you try this:
function copyColumn() {
var ss = SpreadsheetApp.openById("ID_OF_THE_SPREADSHEET");
var sheetFrom = ss.getSheetByName("SHEET_FROM");
var sheetTo = ss.getSheetByName("SHEET_TO");
var values = sheetFrom.getRange(NO_OF_THE_ROW_WHERE_DATA_STARTS, 3, sheetFrom.getLastRow(), 1).getValues();
sheetTo.getRange(NO_OF_THE_ROW_WHERE_DATA_STARTS,sheetTo.getLastColumn()+1,values.length,1).setValues(values);
}
The above script localizes all the data that needs to be copied by using the getRange() method and is copied by using getValues().
NO_OF_THE_ROW_WHERE_DATA_STARTS is the value representing the number of the row where your data starts (in your case the row of No of LP - Secured);
3 is the value representing the C column (where the data you want to be copied is located);
sheetFrom.getLastRow() is the value representing the end of the data you want to be copied;
1 is the value representing the number of columns that need to be copied.
Afterwards, the getRange() method is used again in order to be able to identify where the data needs to be pasted and setValues() in order to actually paste it.
Note: The above script works for different spreadsheets and/or different sheets OR for sheets in the same spreadsheet. If you want to use it for the latter case, you just have to put the name of your sheet instead of SHEET_FROM and SHEET_TO.
Moreover, I suggest you check the following links since they might be of help:
Sheet Class Apps Script - getRange();
Range Class Apps Script - getValues();
Range Class Apps Script - setValues();
Sheet Class Apps Script - getLastColumn();
Sheet Class Apps Script - getLastRow();

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

getRang(x, x) not returning values added to a sheet using a Google Sheet query ( QUERY() )

I'm using the following line of code to get data from a Google Sheet (where sheet is already defined as a Sheet object):
sheetContent = sheet.getRange(6,1).getValue();
I get no content, despite the fact that there is clearly content there. If I use getRange(5,1), then I DO get content of the row.
Cell (5,1) is populated by an INDEX() sheet function.
Cell (6,1) is populated by INDEX(), copying over from a table that has been populated using QUERY().
I suspect that the QUERY() sheet function has not populated the cells with data by the time I try to access contents using getDataRange() in Google App Scripts.
Does QUERY() save data in Google Spreadsheet?
Am I right in this? Otherwise it could be that the query is taking too long and I'm getting an empty range as a result...