【问题标题】:How to insert formula to cell in column with corresponding rownumber?如何将公式插入具有相应行号的列中的单元格?
【发布时间】:2022-12-05 12:29:44
【问题描述】:

在名为“WHATMONTH”的工作表中,我需要自动添加一个公式。范围列 G2:G 的脚本,示例

=月份(A2)

但是将它添加到 G 列所有行中的整个工作表中仍然会给我 A2 / month(A2),它应该是相应的行号,而不是所有行中的 2。

这里我只是猜测:=month(A%[rownumber]),类似的东西。

这可能吗?

【问题讨论】:

  • 为什么不在单元格A2 中输入一个ArrayFormula =ArrayFormula(MONTH($A$2:$A))?你真的不需要每一行都有一个公式来让它工作。
  • 因为我在大约 10 列中具有相同变量需求的数千行中的第 10 行工作。手动添加不再是一种选择,因为工作表最近随着数据的增加而急剧扩展,现在仍然如此。
  • 但是,如果 =ArrayFormula(MONTH($A$2:$A)) 可以用脚本添加 - 固定 - 到 G2,那么你的想法实际上会奏效。
  • 当从工作表顶部添加新数据时,它会将公式向下推,如果没有脚本强制进入 G2,则会失败
  • 听起来主要问题是当输入新数据时行将被下推,如果是这种情况,请将公式更改为=MONTH(INDIRECT("A"&ROW()))

标签: google-apps-script


【解决方案1】:

简而言之,这是您可以根据您的要求使用 appscript 将公式 =ArrayFormula(MONTH($A$2:$A)) 设置到单元格 G2 中的方法:

// But if =ArrayFormula(MONTH($A$2:$A)) could be added with a script - fixed - to G2, then your idea would work, in fact.

const SheetName = 'Your Sheet Name';
const Range = 'G2';
const Formula = '=ArrayFormula(MONTH($A$2:$A))';

function setFormula(){
  SpreadsheetApp // this call the Spreadsheet related functions.
    .getActiveSpreadsheet() // get spreadsheet_object of the active spreadsheet.
    .getSheetByName(SheetName) // get sheet_object of the sheet from the spreadsheet you are working with by sheet name.
    .getRange(Range) // get range_object of the range from the sheet you are working with.
    .setFormula(Formula); // set a formula into the traget range.
}

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 2015-12-29
    • 2021-11-08
    • 1970-01-01
    • 2022-01-24
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多