2017-10-16 57 views
0

目前我的功能看起來像這樣:如何使用1個輸入獲取同一行中單元格的值?

function myFunction(value1, value2, value3, value4, value5, value6) 

基本上VALUE1始終是A列中,而行之間的變化。 value2-6永遠是彼此右邊的1個單元格。即,值1 = A1,這意味着值2 = B1,值3 = B3等或值3 = A5,值2 = B5等

我基本上只是想我的輸入是第一列,我的程序知道讀取值2-6,像這樣:

myFunction(value1) 

我該如何做到這一點?

回答

0

以下內容應該相當清晰和高效。至少它在工作。 請注意,特別是對於像這樣的簡單模式(一行中的所有單元格),應首先定義範圍,然後在數組中一次選取值。一次選取一個單元格的值明顯較慢。

function myFunction(value1) 
{ 
    var cell1 = SpreadsheetApp.getActiveSheet().getRange(value1); 

// myFunction("A1") is the same as following: 
//cell1 = SpreadsheetApp.getActiveSheet().getRange("A1"); 

// Define the value here, if it is fixed. 
//Otherwise in the function parameters 
var columnCount = 6; 

// Define the range to get the values from. Parameters: 
// Starting row of the range : row of Cell1 
// Starting column of the range : column of cell1 
// Number of rows = 1 
// And the number of columns we wanted 
var values = SpreadsheetApp.getActiveSheet() 
         .getRange(cell1.getRow() , cell1.getColumn() , 1 , columnCount) 
         .getValues(); 

// var values == [[ value, value, value, value, value, value ]] 
// so actually values[0] is what you requested, the values from A1 to F1 

// Now if you want to, for example sum up the values 
// You just iterate through the values[0] 

var sum = Number(0); 
for (var i = 0; i < values[0].length; i++) 
{ 
var cellValue = values[0][i]; 

sum += Number(cellValue); 
} 

return sum; 
} 

請記住,當你調用在電子表格單元格的功能,你不能只是單擊單元格添加參數,因爲它會導致因爲這樣:= myFunction的(A1) - 這樣A1增加了使用單元格A1的值作爲參數。

您需要在調用函數時添加引號,如下所示: = myFunction(「A1」) - 使用單元格作爲參數的方式。

+0

我得到的錯誤是類型錯誤:無法調用的未定義「的getRow」。 (第15行,文件「測試」) –

+0

非常抱歉,發佈時不應該改變尿布,我從代碼中刪除了一行。好的,我更正了代碼。請注意,我將參數名稱更改爲value1並添加了第一行。現在應該工作。 – FatFingersJackson

+0

我現在越來越;錯誤 未找到範圍(第3行)。 它看起來像變量不能被讀爲單元格? –

0

我猜測value1不是一個單元格範圍,如谷歌內置Spreadsheet Service已經提供getRange(row, column)getRange().getA1Notation()等人在Range類。

聽起來像你想要腳本搜索column Aargument value1並返回找到的行數據?

function getValueRow (value1) { 
    var sh = SpreadsheetApp.getActive().getSheets()[1]; 
    // Get each cell value in column "A" and flatten for indexOf() use 
    var bound_col = sh.getRange(1, 1, sh.getLastRow()).getValues().join().split(","); 
    // Compare value1 to column "A" data to find its row 
    // `indexOf()` returns the index of the first match 
    var argument_row = bound_col.indexOf(value1); 

    if (argument_row != -1) { 
    // Get the row data of value1 from column "A" to column "F" 
    var row_data = sh.getRange((argument_row + 1), 1, 1, 6).getValues(); 

    Logger.log("%s is in row %s", value1, (argument_row + 1)); 
    Logger.log("The row_data is: %s", row_data); 

    } else { 
    Logger.log("Can not find %s in column A", value1); 
    } 
} 
+0

看起來像不會工作https://stackoverflow.com/questions/12371434/how-do-i-pass-a-cell-argument-from-a-spreadsheet-to-a-custum-function-as-a-範圍 –

+0

將您的問題添加到「自定義函數」而不是腳本應用程序中會有幫助。他們是不同的東西。你從哪裏調用myFunction(value1 ...)?更多詳細信息+腳本中的代碼也會有幫助 –

+0

我覺得我迷路了,沒關係。我只需要像12個參數一樣調用,最終哈哈 –

相關問題