The following should be quite clear and efficient. At least it is working.
Note that especially with a simple pattern like this (all the cells in a row ) you should first define the range, then pick the values at once in an array. Picking the values one cell at a time is noticeably slower.
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;
}
Remember that when you call the function in the Spreadsheet cell, you cannot add the parameter by just clicking a cell, as it would result as this: =myFunction(A1)
- That way A1 adds uses the VALUE of cell A1 as parameter.
You need to add quotation marks when calling the function, like this:
=myFunction("A1")
-That way you use the cell as parameter.