我有一个Google电子表格,使用此github link中给出的脚本从融合表中添加数据 . 我在电子表格的右端添加了两个列,其中一列使用公式填充
=COUNTIF(A$2:A$24,A2)
和其他列的另一个公式 . 目前,每次使用新行更新电子表格时,我都会手动拖动公式列以更新其中的数据 . 是否可以使用脚本动态更新公式列?即,当行添加公式列时也动态更新 .
EDIT:
// evaluate project type and set identifier
function addCountIfFormulaToCell(){
// add the id of your spreadsheet here
var sss = SpreadsheetApp.openById('0AozvCNI02VmpdG5tb0pkUGdDR3djMm5NV0pYeThFbGc');
// add the name of the sheet here
var ss = sss.getSheetByName('Sheet1');
// column you want to evaluate for the formula
var columnToEvaluateAgainst = "A";
// column you want to add the formula to
var columnToAddFormulaTo = "H";
// identifies the last row
var lastRow = ss.getLastRow();
// is the cell to evaluate in the last row
var evaluateThisCell = columnToEvaluateAgainst + lastRow;
// is the cell that gets the forumla in the last row
var addFormulaToThisCell = columnToAddFormulaTo + lastRow;
// this is my formula
var projectFormula = "COUNTIF(A$2:$A,A2)";
// grabs the cell that gets the forumla in the last row
var ssCellToGetFormula = ss.getRange(addFormulaToThisCell);
// sets the formula to the cell in the last row
ssCellToGetFormula.setFormula(projectFormula);
};
2 回答
另一种选择是使用单个数组公式,它将自动填充列:
=ArrayFormula(IF(LEN(A2:A);COUNTIF(A2:A;A2:A);IFERROR(1/0)))
Pradeep ...当您的电子表格中添加了新行时,可以使用apps script triggered来运行 .
下面的脚本是从我用于将IF公式插入到最后一行中用于计算另一列中的值的单元格的脚本中修改的 .
很快:
添加电子表格的ID .
添加工作表的名称 .
我为要评估的列和我将添加公式的列创建了一个变量 . 您可能需要也可能不需要此功能 .
使用.getLastRow()获取电子表格的最后一行 .
创建一个范围,它将精确定位我要评估的单元格以及我将添加公式的单元格
为公式创建变量 .
获取我将添加公式的单元格 .
使用.setFormula将公式添加到该单元格 .
这可能会更有效率,您可能无法直接使用此框,但它会让您了解一些可用的机制 .
克里斯K.