0

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.

4

2 回答 2

0

我想我只是在需要这个方面有点愚蠢。

因为这已经通过脚本完成了,所以我可以简单地将所有 CONTINUE() 公式手动添加到单元格中,一直向下到“根”单元格(其中包含查询的那个)的右侧。如果我在别处使用 UNIQUE() 函数来告诉我有多少不同的酿酒商(或者我正在关注的任何东西),我什至可以很聪明地知道要走多远。这就是 CTRL+E 所做的所有事情——将 CONTINUE() 公式填充到需要的地方。

无论如何,现在一切正常,一旦填充了所有 CONTINUE() 单元格,按下 CTRL+E 的提示就会从“根”单元格中消失。

于 2014-04-24T13:32:32.450 回答
0

在 Google Calc 中,只有当公式自身发生更改时,才能通过脚本重新计算公式。更改公式使用的值不会强制公式重新计算。

解决方案是将脚本“Date.now()”添加到公式中,由始终为真的“IF”条件扩展,并在真时计算请求的部分。

例如:在您的代码中:

   range.setFormula('=IF(true;SORT(TRANSPOSE(QUERY(A2:' + pivotColumn + sheet.getLastRow().toString() + ',"Select count(A) pivot(' + pivotColumn + ')")),' + sortColumn.toString() + ',' + sortAsc + ');"'+Date.now()+'"');
于 2021-08-14T08:45:43.843 回答