自动在Google电子表格列中拖动公式

时间:2013-04-23 07:08:19

标签: google-sheets drag google-fusion-tables auto-update autofill

我有一个Google电子表格,使用此github link中提供的脚本从融合表中添加数据。我在电子表格的右端添加了两个列,其中一列使用公式

填充
    =COUNTIF(A$2:A$24,A2)

和其他列的另一个公式。目前,每次使用新行更新电子表格时,我都会手动拖动公式列以更新其中的数据。是否可以使用脚本动态更新公式列?即,当添加公式列的行也动态更新。

修改

// 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 个答案:

答案 0 :(得分:0)

Pradeep ...当您的电子表格中添加了新行时,您可以使用apps script triggered来运行。

下面的脚本是从我用于将IF公式插入到最后一行中用于计算另一列中的值的单元格的脚本中修改的。

快速:

  • 添加电子表格的ID。
  • 添加工作表的名称。
  • 我为要评估的列和我将添加公式的列创建了一个变量。您可能需要也可能不需要。
  • 使用.getLastRow()获取电子表格的最后一行。
  • 创建一个范围,它将精确定位我要评估的单元格以及我将添加公式的单元格
  • 为公式创建变量。
  • 获取单元格我将添加公式。
  • 使用.setFormula将公式添加到该单元格。

这可能会更有效率,你可能无法直接使用它,但它会让你了解一些可用的机制。

Chris K。

    // evaluate project type and set identifier
    function addCountIfFormulaToCell(){

        // add the id of your spreadsheet here
        var sss = SpreadsheetApp.openById('0ApI9xmBd0k....');

        // add the name of the sheet here
        var ss = sss.getSheetByName('Sheet1');

        // column you want to evaluate for the formula
        var columnToEvaluateAgainst = "B";

        // column you want to add the formula to
        var columnToAddFormulaTo = "C";

        // 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 = "THIS IS MY FORMULA";

        // 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);

    };

答案 1 :(得分:0)

另一种选择是使用单个数组公式,该公式将自动填充列:

=ArrayFormula(IF(LEN(A2:A);COUNTIF(A2:A;A2:A);IFERROR(1/0)))

相关问题