Question

I did find a couple other spots on this, but no answers (or old answers where hopefully things have changed).

I simply want to put a formula in a Google Sheet cell (via a script) and have it fully refresh/recalc. The problem is, the formula returns an array of values, as in:

SORT(TRANSPOSE(QUERY(A2:B299,"Select count(A) pivot(B)")),2,FALSE)

I can make everything work up to the point of placing the formula in the cell I want and selecting that cell. But I still need to manually press CTRL+E to make the array fill in (it is a summary of how many bottles in my beer collection belong to each brewer).

I have tried all the "flush" and re-calc commands I have been able to find via research, and nothing makes the formula "take". Alternately, I looked for a brute force solution -- send a CTRL+E to the cell as if I had pressed the keys myself -- but was unable to find anything that can do something like a Visual Basic "SendKeys" or a Visual Foxpro "KEYBOARD" command.

Here is the Google Script code behind a button that calls "refreshBrewerTable()":

function refreshBrewerTable() {
  return refreshQuery(9, 13, "B", 2, "FALSE");
}

function refreshQuery(rootrow, rootcol, pivotColumn, sortColumn, sortAsc) {
  var range = sheet.getRange(rootrow, rootcol, sheet.getLastRow(), rootcol + 1)
  range.clearContent();
  range = sheet.getRange(rootrow, rootcol);
  range.setFormula('=SORT(TRANSPOSE(QUERY(A2:' + pivotColumn + sheet.getLastRow().toString() + ',"Select count(A) pivot(' + pivotColumn + ')")),' + sortColumn.toString() + ',' + sortAsc + ')');
  sheet.setActiveSelection(range.getCell(1, 1).getA1Notation());
  return true;
}

Any ideas? I basically want to have a button that will refresh this pivoted summary data as I add new beer bottles to my walls.

Was it helpful?

Solution

I think I was just being sort of dumb in regards to needing this.

Because this is already being done from a script, I can simply manually add all the CONTINUE() formulas to cells all the way down and to the right of the "root" cell (the one with the query in it). I can even be smart about how far down to go if I use a UNIQUE() function elsewhere to tell me how many distinct brewers there are (or whatever I am pivoting on). That is all the CTRL+E does -- fills in the CONTINUE() formulas as far down as they need to go.

Anyway, it all works now, and once all the CONTINUE() cells are populated, the prompt for pressing CTRL+E disappears from the "root" cell.

OTHER TIPS

In Google Calc formula will can re-calculated by script only if formula it self will be changed. Changing values, which formula uses do not force formula to recalculate.

The solution is to add in script "Date.now()" to formula, extended by "IF" condition which is always true, and on true calculates requested part.

E.g: in your code:

   range.setFormula('=IF(true;SORT(TRANSPOSE(QUERY(A2:' + pivotColumn + sheet.getLastRow().toString() + ',"Select count(A) pivot(' + pivotColumn + ')")),' + sortColumn.toString() + ',' + sortAsc + ');"'+Date.now()+'"');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top