1

我有一個Google電子表格,使用此github link中給出的腳本將數據添加到融合表中。我添加兩列在其中一個是使用式自動拖動Google電子表格列中的公式

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

}; 

回答

0

另一種選擇是使用單一數組公式,將自動填充下來柱:

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

+0

我想要類似的技術。您的公式將按照我的需要填充列。但問題是,當我使用類似'= Query(Sheet1!A1:$ G,「Select A,Count(E),G GROUP BY A,G Pivot C」)查詢來創建數據透視表時,空行也。例如,我做了一個示例[表單](https://docs.google.com/spreadsheet/ccc?key=0AozvCNI02VmpdG5tb0pkUGdDR3djMm5NV0pYeThFbGc&usp=sharing)。轉到Sheet2查詢結果。 – mpsbhat 2013-04-24 05:22:30

+0

'= QUERY(Sheet1!A:G;「選擇A,計數(E),G其中G不是空組A,G樞軸C」) – AdamL 2013-04-24 20:27:11

+0

它起作用..!謝謝@AdamL。 – mpsbhat 2013-04-25 04:27:27

0

普拉迪普......你可以使用一個apps script那就是當一個新的行添加到您的電子表格triggered運行。

下面的腳本是從我用來插入一個IF公式到最後一行的單元格中的另一列中的值進行計算的。

快速:

  • 添加您的電子表格的ID。
  • 添加工作表的名稱。
  • 我爲我將要評估的列和將要添加公式的列做了一個變量。你可能會也可能不需要這個。使用.getLastRow()獲得電子表格的最後一行。
  • 創建一個範圍,它將查明我想評估的單元格以及將添加公式的單元格
  • 爲公式創建一個變量。
  • 獲取我將要添加公式的單元格。
  • 使用.setFormula將公式添加到該單元格。

這很可能是更爲有效的,你可能無法使用這個開箱,但它會給你一些可用的mechanisims的想法。

克里斯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); 

    }; 
+0

我已經加入你的腳本在我的[示例電子表格] (https://docs.google.com/spreadsheet/ccc?key=0AozvCNI02VmpdG5tb0pkUGdDR3djMm5NV0pYeThFbGc&usp=sharing),並且該函數運行時沒有錯誤。但是這些數據並未填寫在專欄中。我添加的代碼在上面的問題中進行了編輯。 – mpsbhat 2013-04-24 05:35:40

相關問題