Accessing the Cell Coordinates in custom function - google-apps-script

Is there a way to access the cell coordinates (in A1 notation) of the cell that was passed in to my function?
For example, if my function is this
function displayA1Notation(myCell){
return myCell.getA1Notation();
}
and I put the following in cell B4:
=displayA1Notation(C6)
I'm hoping to see this:
C6
But what I actually see is this:
Kansas
("Kansas" is the actual cell value of C6)
I know this seems easy... I'm just stuck in trying to get it to work.
Thanks~!

Spreadsheet custom functions arguments contain only values pointed as arguments and not cell addresses. This fact not clear documented but there are a couple of similar questions here, for instance, this one. There is a workaround by using the build-in function ADDRESS. like in the following code
=myFunc(ADDRESS(ROW(F8), COLUMN(F8)))

Related

Google Scripts - using variable with .getRange() function?

Basically, I want to the user to be able to input their selected cell without hard coding.
Instead of what I have now: newFileBody.getRange('g4').setValue(email);
I have a config sheet where the user puts in values, and I'm pulling their response into this file. The user will type in what cell they want the information to be put in (let's say they also type 'g4', and that will be stored as "user_Variable").
I'd like it to be: newFileBody.getRange(user_Variable).setValue(email); where "user_Variable" is a specific cell they've typed in on the config sheet.
The getRange() method can accept a string variable as long as it's a valid R1C1 notation, such as G4.
Sample:
function myFunction(userVariable) {
// some code..
newFileBody.getRange(userVariable).setValue(email);
// some more code..
}
Documentation:
getRange() A1 Notation

Evaluate a string to a formula in Google Sheets

I am writing a math paper where i would like to display my calculations (formulas) separately from the solution.
I am currently working in Google Sheets.
The end goal would be to have one column with formulas and one column with answers.
I tried to work with GS to write a function that would take the string value from A1 and evaluate it in the B1 column.
I used this simple script that i found on:
https://support.google.com/docs/thread/13826624/evaluate-string-as-formula?hl=en
function run(input){
return eval(input);
}
It works with simple calculations like division, multiplication, addition and subtraction.
But the script doesn't solve basic exponents like 1 * 10^3 (it gives me 9). And square roots like sqrt(9) (gives me an #error)
I'm not sure which way to go from here.
The easiest solution would probably be to work the other way around: write your formulas normally, and use the formulatext() spreadsheet function to display the formula in an adjacent cell.

Calling Background colors from google sheets using Google sheets api is missing data

From a previous question linked here ( Previous Question ) I learned about Sheets.SpreadSheets.get calling a JSON of sheet data that would allow me to get the backgroundcolors of a sheet within my project. Id previously been doing this with var BackgroundColors = ActiveWeekSheet.getDataRange().getBackgrounds(); but was told that the JSON method would be a faster read/write method. They directed me to do some reading on Javascript objects but after that I'm still confused.
I've got the following code. TestArray = Sheets.Spreadsheets.get("1irmcO8yMxYwkcLaxZd1cN8XsTIhpzI98If_Cxgp1vF8"); which seems to call a JSON with sheet specific data. A logger statement of TestArray returns this: testArrayObject: {"properties":{"gridProperties":{"rowCount":1000,"columnCount":26},"sheetType":"GRID","index":0,"sheetId":0,"title":"Awesome"}}
Community members previously suggested I could then find the background colors at: sheets[].data[].rowData[].values[].cellData.effectiveFormat.backgroundColor
I've highlighted one of the cells yellow but when reviewing the above JSON i can't seem to find anything that references color. There definitely isn't any multileveling of the JSON to refer to sheets->data->rowData->values->celldata.effectiveFormat.backgroundColor.
What am I missing here? Do I need to format things someway? Am I not calling the right JSON to start with?
Thanks!
As written in the documentation,
By default, data within grids will not be returned. You can include grid data one of two ways:
Specify a field mask listing your desired fields using the fields URL parameter in HTTP
Sheets.Spreadsheets.get(spreadsheetId, {
ranges:"Sheet1!A1:A5",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
})
Set the includeGridData URL parameter to true. If a field mask is set, the includeGridData parameter is ignored
Sheets.Spreadsheets.get(spreadsheetId, {
ranges:"Sheet1!A1:A5",
includeGridData: true
})
Field mask documentation:
In a nutshell,
multiple different fields are comma separated, and
subfields are dot-separated.
For convenience, multiple subfields from the same type can be listed within parentheses.
You may test the API here
There are optional parameters in the spreadsheets.get method that will give you that data, but you need to explicitly include them:
ranges – The ranges to retrieve from the spreadsheet.
includeGridData – The cell data within specified range.
This specifies a range of just one cell (A1 in Sheet1), but you can specify a larger range and navigate through the array if you need to.
var TestArray = Sheets.Spreadsheets.get(SS_ID, {ranges: "Sheet1!A1", includeGridData: true});
Really important that you keep in mind this returns a Color object with RGBA
values that range from 0-1, but elsewhere apps script uses hex color or the conventional 0-255 RGB values.

How to query an array in Google Sheets?

In Google Sheets i have in a cell an array like [27, https://www.example.com/page1.html, false, false, 1]
How can i access its single parts with a formula?
I know a way through =SPLIT(), like =split(split(A2,"["),",") - but i would very like, if its possible, to access each part directly (each array has always the same amount of parts in my data set).
Maybe something like =QUERY(query(A2,",",1)) - cell, divider, item number...? - Result is 27.
=INDEX(SPLIT(A2,"[,]"),1)
SPLIT by each of these characters [,]
INDEX into the resulting array
I would like to take the chance and propose a solution using Google Apps Script. Basically, you can create your own custom function to accomplish this task.
Please follow these steps:
Go to Tools => Script editor from your spreadsheet file:
Clear the default Code.gs file, copy and paste this function and save the changes:
function indexArray(arr,pos) {
return array=arr.slice(1,-1).split(",")[pos-1]
}
You are now able to access the indexArray() function from within your spreadsheet file. It accepts two arguments, the desired cell that contains the array and the position of the element you would like to access, starting from 1:
=indexArray(A2,2)
For example, this will give you the second element of your array which is: https://www.example.com/page1.html.
Check these instructions out if you need more information how custom functions work. They are pretty straightforward.

Is it possible to determine if a number is recursive in Google Sheets?

So I'd like 1/3 (which equals 0.33333 recurring) to return true and 1/8 (which equals 0.125 non-recurring) to be false.
Something like =ISRECURRING(A1) which returns a boolean.
The reason for this is that I want to highlight cells that have had rounding applied to them.
You can build a JavaScript function to check that and use it in your sheet as an Apps Script custom function. To achieve this, follow these steps:
In your spreadsheet, select Tools > Script editor to open a script bound to your file.
Copy this function in the script editor, and save the project (credits to Larry Battle, whose answer here this function is based on):
function ISRECURRING(num) {
num = (num || "").toString().replace(/\d$/, '');
var RE_PatternInRepeatDec = /(?:[^\.]+\.\d*)(\d{2,})+(?:\1)$/;
return RE_PatternInRepeatDec.exec(num) ? true : false;
};
Now, if you go back to your spreadsheet, you can use this function as if you were using a regular sheets formula. You just have to provide the appropriate range as an argument, as you can see here:
Note:
As #Ron Rosenfeld noted in a comment below, this function will not work for very long repetends (e.g. 1/97). This is due to the precision limit that spreadsheets have (15 digits, as far as I know). Because of this, repetends longer than 7 digits won't be detected.
Reference:
Custom Functions in Google Sheets
Credit to #Ron Rosenfeld.
=A1-ROUND(A1,14) <> 0 works a treat. Where A1 is the number in question.
I can't guarantee that every rounded number works but it appears to work on all the examples I've tried.