简体   繁体   中英

Saving data in Google Sheets with Google Apps Script

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?

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

I found some issues on your code:

  1. 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].

  1. In Sheet Service, each array is represented as row in Google Sheets. appendRow() can only accepts 1D array which means appendRow() 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]]
  1. To retrieve multiple values excluding the empty cells, you can use getRange(row, column, numRows, numColumns) with the help of getLastRow() as numRows .

  2. To append multiple values, you have to use setValues() with the help of getRange(row, column, numRows, numColumns) and array.length . numRows can be determine by counting the number of sub-array in your 2d array or array.length and row can be determine by using getLastRow() .

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

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM