As far as I know, this is not possible
But there is a workaround (or better way to do it, depends how you look at it).
There is no INDIRECT
specifically for formulas, however:
Workaround
A function exists in Apps Script:
formulaCell.setFormula(formulaString)
You don't need much Apps Script to get this working in the way you envision it, the Tutorials can be helpful to get started.
I would also recommend this approach as formulae will quickly become limiting once you cover more complex ground, and will also be much, much easier to maintain and modify. You also get the added benefit of longer execution times.
Sample script
function createFormula() {
// initializing variables for the Spreadsheet file and the sheet
let file = SpreadsheetApp.getActive();
let sheet = file.getSheetByName("Sheet1");
// This is the range that represents the sheets to filter
let sheetsToProcessRange = sheet.getRange("C2:C");
let sheetsToProcess = sheetsToProcessRange.getValues();
// filter out the empty ones
sheetsToProcess = sheetsToProcess.filter(row => row[0])
// At the moment its a 2D array
// [["Sheet1"],["Sheet2"],["Sheet3"]]
// This will change it to a 1D array (just to make code easier to read)
// ["Sheet1","Sheet2","Sheet3"]
sheetsToProcess = sheetsToProcess.map(row => row[0])
// initializing the array notation strings
let filterArrayString = "{";
let lenArrayString = "{"
// for each sheet in the array, add SheetX!Range to the strings
sheetsToProcess.forEach(sheetName => {
filterArrayString += `${sheetName}!A2:H;`
lenArrayString += `${sheetName}!A2:A;`
})
// Remove trailing semicolon
filterArrayString = filterArrayString.slice(0, filterArrayString.length - 1)
lenArrayString = lenArrayString.slice(0, lenArrayString.length - 1)
// Add in final bracket
filterArrayString += "}"
lenArrayString += "}"
// construct whole formula string
let formulaString = `=arrayformula(filter(${filterArrayString},len(${lenArrayString})))`
// Choose the cell for it and set it as a formula
let formulaCell = sheet.getRange("A9")
formulaCell.setFormula(formulaString)
}
This, run from the script editor will insert your desired formula in cell A9
in Sheet1
.
I have used some more advanced features of JavaScript to do this succinctly but you don't have to do it this way.
You already know how to construct most of the formula with formulae. So you could have a script that just grabs this value and so would shorten the script considerably.
function createFormulaSimplified(){
// Initializing variables
let file = SpreadsheetApp.getActive();
let sheet = file.getSheetByName("Sheet1");
// Range where your already constructed formula lives
let formulaCell = sheet.getRange("A1")
let formulaString = formulaCell.getValue()
// Constructing and setting the formula
let formulaString = "=" + formulaString
let formulaCell = sheet.getRange("A9")
formulaCell.setFormula(formulaString)
}
In effect, this does the same thing as the formula above, but less of the work is done by the script. Basically the only thing it does is grab the formula you have already constructed, adds an =
to the start and assigns it to cell A9
.
Of course, you could also do all the work that the formula does in Apps Script, but that is for another answer of another question.
References