Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 665
soneangel Avatar asked Mar 15 '16 13:03

soneangel


People also ask

How do I use a string formula in Google Sheets?

When you need to construct a formula instead of directly type a formula, use the INDIRECT function. It turns text strings into a cell reference. For example, if you need to have a formula that references a column of data. You may want the same formula for a different column.

Can you evaluate a formula in Google Sheets?

No, there's no equivalent to Excel's EVALUATE() in Google Sheets.

How do I convert a string to a cell reference in Google Sheets?

VLOOKUP + INDIRECT Formula in Google Sheets Create a Google Sheets drop down menu to let the user select one of these sheet names and then use the INDIRECT function to convert the text string into a valid range reference which becomes your lookup table.


1 Answers

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);
}
like image 145
Rubén Avatar answered Sep 20 '22 05:09

Rubén