I am trying to save data to Google Sheets using the following function:
// function to save data
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var refresh_time = sheet.getRange('Testdata!A2:A').getValue();
sheet.appendRow([refresh_time]);
}
The problem is that only cell A2 is saved, but not the whole range A2:A This is probably due to the appendRow. Is there also a command like appendRange?
CodePudding user response:
To get the values in A2:A you should use getValues() and not appendRow but setValues(). However I don't think that is what you want. A2:A are all the cells from column A except the first and may include empty cells.
Instead try this:
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheets()[0];
var sheet2 = ss.getSheetByName("Testdata");
var refresh_time = sheet2.getRange(2,1,sheet2.getNumRows()-1,1).getValues();
sheet1.getRange(sheet1.getLastRow() 1,1,refresh_time.getLastRow(),refresh_time.getLastColumn()).setValues(refresh_time);
}
CodePudding user response:
I found some issues on your code:
- If you check the usage of
getValue(), it will only return a single cell (top left cell of the range provided).
getValue() - Returns the value of the top-left cell in the range. The value may be of type Number, Boolean, Date, or String depending on the value of the cell. Empty cells return an empty string.
Instead you should use getValues() to retrieve values of multiple cells.
getValues() - Returns a two-dimensional array of values, indexed by row, then by column. The values may be of type Number, Boolean, Date, or String, depending on the value of the cell. Empty cells are represented by an empty string in the array. Remember that while a range index starts at 1, 1, the JavaScript array is indexed from [0][0].
- In Sheet Service, each array is represented as row in Google Sheets.
appendRow()can only accepts 1D array which meansappendRow()can only append single row. Having multiple rows means multiple arrays or 2D array.
Example:
If you retrieve the values of A1:A8, it will return:
[[1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0], [8.0]]
To retrieve multiple values excluding the empty cells, you can use
getRange(row, column, numRows, numColumns)with the help ofgetLastRow()asnumRows.To append multiple values, you have to use
setValues()with the help ofgetRange(row, column, numRows, numColumns)andarray.length.numRowscan be determine by counting the number of sub-array in your 2d array orarray.lengthandrowcan be determine by usinggetLastRow().
Nitpick:
This lines of code works:
var sheet = ss.getSheets()[0];
var refresh_time = sheet.getRange('Testdata!A2:A').getValue();
but it is more efficient if you declare another variable for handling Testdata sheet.
Your final code should look like this:
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var testDataSheet = ss.getSheetByName("Testdata");
var refresh_time = testDataSheet.getRange(2, 1, testDataSheet.getLastRow()-1).getValues();
sheet.getRange(sheet.getLastRow() 1, 1, refresh_time.length, 1).setValues(refresh_time);
}

