Google sheets custom function argument address for conditional formatting - google-apps-script

I'm writing a custom function in Google Sheets with intention of using it for conditional formatting:
function f(cellValue) {
}
How can i get the address of the cell whose value i get in the cellValue argument?
The function should return true if the three cells above the given one are empty.

As far as I know, you can't do it directly.
But your function could take 2 more arguments:
function f(cellValue, rowNum, columnNum) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange(rowNum, columnNum);
var address = range.getA1Notation();
return address + " = " + cellValue;
}
Example of the usage of the formula:
= f(A1, row(A1), column(A1))
So we use native functions (row & column) inside custom formula.
OK, let's talk about your task.
The function should return true if the three cells above the given one are empty.
You may use this formula (no script needed):
=join("",OFFSET(B63,-3,,3))=""

Related

Call cell data from different spreadsheets WITHOUT having to go to the sheet and select the cell

I would like to create a function in script editor that would allow me to input data from a cell on another sheet without having to put 'sheet1!A1' in the formula.
Say I have my sheet name in A1 and want to return the number in the first row and first col (i.e. A1) of that sheet. I would like to show that value with a formula as simple as getcell(A1,1,1)
Here's what I've got but I get an error saying "Exception: Range not found (Line 4)"
function getcell(sheetName,row, col) {
var ss = SpreadsheetApp.getActive()
var range = ss.getSheetByName(sheetName).getRange(row, col)
ss.getRange(range).getValue();
}
I believe your goal is as follows.
You want to return A1 as A1Notation by putting =getcell(A1,1,1) to a cell.
You want to achieve this using the custom function.
In this case, how about the following modification?
Modified script:
function getcell(sheetName, row, col) {
var ss = SpreadsheetApp.getActive()
var range = ss.getSheetByName(sheetName).getRange(row, col);
var res = range.getA1Notation();
return res;
}
In your script, range is used in getRange(range). By this, such an error occurs.
In order to return the A1Notation, getA1Notation() is used.
If you want to return the cell value, please modify getA1Notation() to getValue().
Reference:
getA1Notation()
Three small corrections:
You need to pass the sheet name in quotes
You are trying to get the range twice
For a custom funciton, you need to return the value
Sample code:
function getcell(sheetName,row, col) {
var ss = SpreadsheetApp.getActive()
var range = ss.getSheetByName(sheetName).getRange(row, col)
return range.getValue();
}
Sample call:
=getcell("Sheet1",1,1)

Copy Paste into formula as apps script

I'm trying to run a google apps script which takes a series of importrange functions and puts them into an arrayformula query. I've successfully created a cell which actively accumulates the correct links for use in the importrange and puts it into a cell as a string. All I need the script to do is to copy that string and paste it as a formula in another cell. I can do this manually pretty easily, but I'd like to be able to set it up on a timer so it does it automatically on a certain time period.
As far as I've gotten is below and it doesn't work at all:
function Update Import Ranges() {
var spreadsheet = SpreadsheetApp.getActive();
var source = spreadsheet.getRange('B2').activate();
spreadsheet.getCurrentCell('B3').setFormula(???);
spreadsheet.getRange('B3').activate();
};
accumulates the correct links for use in the importrange and puts it into a cell as a string
The importrange() function only accepts two parameters: one for the spreadsheet to read from, and another to specify which range in the spreadsheet to return. To read from several spreadsheets and/or ranges with importrange(), you will have to create several formulas.
Alternatively, use an Apps Script function that mimics importrange() but supports multiple spreadsheets and ranges, such as the ImportMultipleRanges() function. You could call it like this:
function updateDataFromOtherSpreadsheets() {
var rangeToUpdate = "All Data!A2";
var sourceSpreadsheetKeysRange = "Config!B2:B5";
var sourceSpreadsheetDataRange = "Config!C2:C5";
var add_spreadsheet_name_prefix = false;
var add_sheet_name_prefix = false;
var ignore_blank_rows = true;
var ss = SpreadsheetApp.getActive();
var spreadsheet_keys = ss.getRange(sourceSpreadsheetKeysRange).getValues();
var range_strings = ss.getRange(sourceSpreadsheetDataRange).getValues();
var data = ImportMultipleRanges(spreadsheet_keys, range_strings, add_spreadsheet_name_prefix, add_sheet_name_prefix, ignore_blank_rows);
ss.getRange(rangeToUpdate).offset(0, 0, data.length, data[0].length).setValues(data);
}
To answer your question, you can fix the syntax errors and semantics in your function like this:
function updateImportrangeFormula() {
const sheet = SpreadsheetApp.getActiveSheet();
const ssId = sheet.getRange('B2').getValue();
const rangeA1 = sheet.getRange('C2').getValue();
const formula = '=importrange("' + ssId + '", "' + rangeA1 + '")';
sheet.getRange('B3').setFormula(formula);
}

Get a non-contiguous active range by selection and use it programmatically Google script

I want to get a non-contiguous range by selection and use it programmatically.
If I select say, A1 to F10 so I have a Contiguous range I can use below and it will give A1:F10
function getSelectedRange_Contiguous(){
var selected = SpreadsheetApp.getActiveSheet().getActiveRange(); // Gets the selected range
var rangeString = selected.getA1Notation(); // converts it to the A1 type notation
Logger.log(rangeString)
return rangeString;
}
If I use the macro recorder and select A1:A10 & C1:C10 & E1:E10 I get a function that will select a discontinuous range
function UntitledMacro() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRangeList(['A1:A10', 'C1:C10', 'E1:E10']).activate();
};
But how do you get a Non-Contiguous range from selection to be used programmatically
Non-Contiguous does not work, it gives E1:E10
function getSelectedRange_NonContiguous() {
var spreadsheet = SpreadsheetApp.getActive();
var selected = spreadsheet.getActiveRange().getRangeList([]);
var rangeString = selected.getA1Notation();
Logger.log(rangeString)
return rangeString;
};
Explanation:
You need to use getActiveRangeList() instead of getActiveRange to get the Non-Contiguous active range.
Also it is better if you apply this method to a particular sheet instead of the spreadsheet object so you can later on be more specific on the sheet object.
Solution:
function getSelectedRange_NonContiguous() {
const sheet = SpreadsheetApp.getActive().getActiveSheet();
const selected = sheet.getActiveRangeList().getRanges();
// an array of selected ranges
const notations = selected.map(rng=>rng.getA1Notation());
// log them:
notations.forEach(nt=>console.log(nt));
return notations;
};
Keep in mind that if you are planning to use that function as a custom function (a custom formula in your sheet) you won't get live updates because of the custom formula restrictions.

Convert a string to a formula in Google Spreadsheet

I´d been more than 10 hours reading and trying different options with no success.
I have this string (this is actually a string that is generated by other formulas)
QUERY({IMPORTRANGE(A1;$D$1);IMPORTRANGE(A2;$D$1);IMPORTRANGE(A3;$D$1);IMPORTRANGE(A4;$D$1)};"select Col13, sum(Col1), sum(Col2), sum(Col3), sum(Col4), sum(Col5), sum(Col6), sum(Col7), sum(Col8), sum(Col9), sum(Col10), sum(Col11), sum(Col12) group by Col13";0)
And I want it to be read as a formula.
So for example I try this function:
function doConvert(formula) {
// Strip leading "=" if there
if (formula.charAt(0) === '=') formula = formula.substring(1);
return eval(formula);
}
But I get:
Error / SyntaxError: Falta ":" detrás del ID de propiedad.
(In English would be: ":" missing after property ID.
Any other solution would be great.
add = to your generated string and try like this:
function onEdit() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet_Name_Here');
var src = sheet.getRange("A1"); // The cell which holds the formula
var str = src.getValue();
var cell = sheet.getRange("C5"); // The cell where you want the results to be in
cell.setFormula(str);
}
Google Sheets formulas can't be evaluated on the server/client code, only on the Google Sheets UI.
If you are looking that your string be passed as a formula to a cell, then use the setFormula(formula) method from the Class Range
NOTE: there is no need to preppend the equal sign to the formula.
Example
Assuming that the formula string is in A1 and that you want to put is as formula in B1
function setFormula(){
var sheet = SpreadsheetApp.getActiveSheet();
var source= sheet.getRange('A1');
var formula = source.getValue();
var target = sheet.getRange('B1');
target.setFormula(formula);
}
Related
Is there a way to evaluate a formula that is stored in a cell?
How to evaluate a spreadsheet formula within a custom function?

Need help Google Apps Script and sheet: Building a custom function for Google spreadsheet to Copy/Paste Value

I'm building a function to get Value from another cell. It will work this way:
At cell A1, I place function = copyValue(B1) or = copyValue(1 , 2) so that custom function will return the value of B1.
Here is my code:
function copyValue(int a,b) {
var ss = SpreadsheetApp.openById('1HTEuyd7po43VKM37nmzCvPgLEVESgcN5YpAu2VRTiFI');
var sheet = ss.getSheetByName("Total");
var cell = sheet.getRange(a,b);
var c = cell.getValue();
return(c);
}
But when I run, it said:
[quote]Missing ) after formal parameters. (line 1)[/quote]
if I remove "int" before the formal params, it said I cannot getRange NULL.
Please help me to fix this. Thanks
You can make your function much simpler if all you are trying to do is get the value of a cell.
function copyValue(cell) {
return cell;
}
Then in your sheet just do =copyValue(B1) and whatever your value is in B1 will be returned into the cell.