2016-08-12 53 views
1

使用公式我想用一個公式自定義函數裏面,像這樣的例子:內部腳本

function myFunction(range, value) { 
    var countNumber = COUNTIF(range; value); // COUNTIF is a formula that can be used in the spreadsheet 
    if (countNumber > 0) { 
     return "RESULT"; 
    } else { 
     return "OTHER RESULT"; 
    } 
} 

然後:

=MYFUNCTION(A1:A5,"VALUETOTEST")


我想簡化一個巨大的公式:

類似於:

=IF(SUM(COUNTIFS(G182:G186;"ERROR";H182:H186;"62");COUNTIFS(G182:G186;"ERROR";H182:H186;"ALL"))>0;"ERRO";IF(SUM(COUNTIFS(G182:G186;"RETEST";H182:H186;"62");COUNTIFS(G182:G186;"RETEST";H182:H186;"TODOS"))>0;"RETEST";IF(COUNTIF(G182:G186;"UNIMPLEMENTED")>0;"UNIMPLEMENTED";"SOLVED"))) 
+0

如果已經解決了您的問題或提供了深入的見解,請不要忘記接受答案。這有助於他人找到你的問題和解決方案。 – MasterCrander

回答

0

這就是我如何解決我的問題。我感謝幫助我達到這個結果的人!

// Like COUNTIFS 
var countConditionals = function(cells, condition1, condition2) { 

    var count = 0; 

    for (i = 0; i < cells.length; i++) { 

    if (cells[i][0] == condition1 && cells[i][1] == condition2) { 
     count++; 
    } 

    } 

    return count; 
} 

// Like COUNTIF 
var countConditional = function(cells, condition) { 

    var count = 0; 

    for (i = 0; i < cells.length; i++) { 

     if (cells[i][0] == condition) { 
      count++; 
     } 

    } 

    return count; 
} 

//Whole Formula 
function verificaStatus(cells, db) { 

    const ERROR = "ERROR"; 
    const ALL = "ALL"; 
    const RETEST = "RETEST"; 
    const NOTYET = "UNIMPLEMENTADED"; 
    const SOLVED = "SOLVED"; 

    var countErrors = countConditionals(cells, ERROR, db); 
    var countErrorsAll = countConditionals(cells, ERROR, ALL); 
    var sumErrors  = countErrors + countErrorsAll; 

    if (sumErrors > 0) { 
    return ERROR; 
    } else { 

    var retest = countConditionals(cells, RETEST, db); 
    var retestAll = countConditionals(cells, RETEST, db); 
    var sumRetest = retest + retestAll; 

    if (sumRetest > 0) { 
     return RETEST; 
    } else { 

     var countNonCreated = countConditional(cells, NOTYET); 

     if (countNonCreated > 0) { 
     return NOTYET; 
     } 

    } 

    } 

    return SOLVED; 

} 
1

谷歌應用腳​​本中的自定義函數無權訪問電子表格函數。您可以嘗試使用此=IF(COUNTIF(A1:A5,"VALUETOTEST")>0,"RESULT","OTHER RESULT")

如果對結果的巨大的公式,嘗試的結果創建函數

function result1() { 
    return "RESULT"; 
} 

function result2() { 
    return "OTHER RESULT"; 
} 

然後用這個=IF(COUNTIF(A1:A5,"VALUETOTEST")>0,RESULT1(),RESULT2())

試試這個 - 複製下面的功能在Google Apps腳本,並將其用作公式=myFunction("G182:G186","H182:H186")記住用'"'來限定範圍,因爲您將以字符串形式傳遞範圍,並注意兩個範圍的長度必須相等。

function myFunction(aRange, bRange) { 

    var cond_1 = "ERROR"; 
    var cond_2 = "62"; 
    var cond_3 = "ALL"; 
    var cond_4 = "RETEST"; 
    var cond_5 = "TODOS"; 
    var cond_6 = "UNIMPLEMENTED"; 


    var sheet = SpreadsheetApp.getActiveSpreadsheet(); 
    var aRange = sheet.getRange(aRange); 
    var aValues = aRange.getValues(); 
    var bRange = sheet.getRange(bRange); 
    var bValues = bRange.getValues(); 
    var count = 0; 
    var tmplength = 0; 

    if (aValues.length != bValues.length) { 
     return "Range length does not Match"; 
    } 

    for (i = 0; i < aValues.length; i++) { 

     if (aValues[i] == cond_1 && bValues[i] == cond_2) { 
      count += 1; 
     } 
     if (aValues[i] == cond_1 && bValues[i] == cond_3) { 
      count += 1; 
     } 

     if (count > 0) { 
      return "ERROR"; 
     } else { 
      count = 0; 

      if (aValues[i] == cond_4 && bValues[i] == cond_2) { 
       count += 1; 
      } 
      if (aValues[i] == cond_4 && bValues[i] == cond_5) { 
       count += 1; 
      } 

      if (count > 0) { 
       return "RETEST"; 
      } else { 
       count = 0; 

       if (aValues[i] == cond_6) { 
        count += 1; 
       } 
       if (count > 0) { 
        return "UNIMPLEMENTED"; 
       } else { 
        return "SOLVED"; 
       } 
      } 
     } 
    } 
} 
+0

對,但這是因爲我有一個巨大的公式。 – Shura16

+0

還有另外一種方法可以做到這一點嗎? – Shura16

+0

您必須在GAS中重新創建公式作爲函數 –

1

您有三種執行這些操作的方法。

1)在您需要的範圍內將圖表公式添加到圖紙本身。然後使用GAS功能從結果單元格中讀取數據(無論您將其設置爲寫入)。然後您可以使用結果執行進一步的處理。

2)使用GAS功能將圖表公式寫入表格。然後使用更多的GAS讀取結果並處理數據。方法可以在這裏找到:https://developers.google.com/apps-script/reference/spreadsheet/range#setFormula(String)

3)您可以使用GAS創建一個自定義圖表公式,然後使用您的工作表。然後GAS可以讀取結果並處理信息。這需要對JS進行一些研究,以瞭解如何重新創建,合併和執行需要表單中的數據執行的操作。

你可以找到一個指南,使這裏自定義公式:https://developers.google.com/apps-script/guides/sheets/functions

和導向,以JS這裏:http://www.w3schools.com/js/default.asp

W3學校有一個比較全面的指導JS。 GAS使用所有原生JS方法,因爲它是JS編碼環境。查看GAS參考資料以獲取更多關於GAS特定方法的信息,這些方法可能會執行您所需的任務

如果你需要的是檢查的條件和/或通過迭代行,嘗試這樣的事情:

function myFunction() { 
var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getActiveSheet(); 
var range = sheet.getRange(startRow, startColumn, numRows, numColumns); 
var values = range.getValues(); //This is a 2D array; iterate appropriately 
    for (i = 0; i < values.length; i++) { 
    if (values[i] == conditionToCheck) { 
     //perform code..OR 
     //continue; <- This works to skip the row if the condition is met 
    } else { 
     //perform alternate code if condition is not met 
    } 
    } 
} 

正如我所說,.getValues()創建一個二維數組。如果您需要通過行和列進行迭代,你需要像這樣2個for()循環:

for (i = 0; i < values.length; i++) { //iterates through the rows 
    for(j = 0; j < values[i].length; j++) { //iterates through the columns in that current row 

它提氣如何處理二維數組是很重要的。 values[i][j]表示有多少行i列和j列。你可以可視化,像這樣:

values = [[A1, B1, C1],[A2, B2, C2],[A3, B3, C3]]

這是陣列,其中外陣列是行的陣列的陣列,而內部是細胞值的該行中的陣列通過柱。

+0

。我將不得不根據情況對細胞進行計數。但爲此,我將不得不閱讀一系列細胞。我需要在單元格中使用foreach。有'sheet.getRange()',但它如何趨向下一個單元格?我怎麼知道它是在範圍的最後? – Shura16

+0

你正在尋找的是一個for(){'循環以及if(){} else {'語句。 for(){'循環允許遍歷一個範圍; 'if()'語句在每次迭代中檢查一個條件。我將編輯我的答案以反映一個例子。如果這個例子足夠了,請接受答案來解決問題。 – MasterCrander