简体   繁体   English

使用 Google Apps 脚本将数据保存在 Google 表格中

[英]Saving data in Google Sheets with Google Apps Script

I am trying to save data to Google Sheets using the following function:我正在尝试使用以下 function 将数据保存到 Google 表格:

// 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.问题是只有单元格 A2 被保存,而不是整个范围 A2:A 这可能是由于 appendRow。 Is there also a command like appendRange?还有像appendRange这样的命令吗?

To get the values in A2:A you should use getValues() and not appendRow but setValues().要获取 A2:A 中的值,您应该使用 getValues() 而不是 appendRow 而是 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. A2:A 是 A 列中除第一个以外的所有单元格,并且可能包括空单元格。

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()的用法,它只会返回一个单元格(所提供范围的左上角单元格)。

getValue() - Returns the value of the top-left cell in the range. getValue() - 返回范围内左上角单元格的值。 The value may be of type Number, Boolean, Date, or String depending on the value of the cell.该值可能是 Number、Boolean、Date 或 String 类型,具体取决于单元格的值。 Empty cells return an empty string.空单元格返回一个空字符串。

Instead you should use getValues() to retrieve values of multiple cells.相反,您应该使用 getValues() 来检索多个单元格的值。

getValues() - Returns a two-dimensional array of values, indexed by row, then by column. getValues() - 返回值的二维数组,按行索引,然后按列。 The values may be of type Number, Boolean, Date, or String, depending on the value of the cell.这些值可能是 Number、Boolean、Date 或 String 类型,具体取决于单元格的值。 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、1 开始,但 JavaScript 数组的索引从 [0][0] 开始。

  1. In Sheet Service, each array is represented as row in Google Sheets.在工作表服务中,每个数组在 Google 工作表中都表示为行。 appendRow() can only accepts 1D array which means appendRow() can only append single row. appendRow()只能接受一维数组,这意味着appendRow()只能 append 单行。 Having multiple rows means multiple arrays or 2D array.多行意味着多个 arrays 或二维数组。

Example:例子:

在此处输入图像描述

If you retrieve the values of A1:A8 , it will return:如果您检索A1:A8的值,它将返回:

[[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 .要检索除空单元格之外的多个值,您可以在getLastRow() as numRows的帮助下使用getRange(row, column, numRows, numColumns)

  2. To append multiple values, you have to use setValues() with the help of getRange(row, column, numRows, numColumns) and array.length .对于 append 多个值,您必须在getRange(row, column, numRows, numColumns)array.length的帮助下使用setValues() 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() . numRows可以通过计算二维数组或array.length中的子数组的数量来确定,而row可以通过使用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.但如果您声明另一个变量来处理Testdata表,则效率更高。

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:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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