简体   繁体   English

使用Sheets API v4仅在公式后附加新行

[英]Appending a new row with only formulas using Sheets API v4

I'm pretty new to Sheets API and get a lot of bugs. 我对Sheets API相当陌生,并且遇到了很多错误。

I would like to append a new row to sheet based on last row. 我想将新行添加到基于最后一行的工作表中。 This would include copying the format and pasting formulas with an autofill but not the values. 这将包括使用自动填充而不是值复制格式和粘贴公式。

Here what I've came up using app script. 这是我使用应用程序脚本编写的内容。 I'm sure I'm not using the best way so for the moment I've 我确定我没有使用最好的方法,所以暂时

  • retrieved formulas from range SUCCESS 从成功范围检索公式
  • tried using autoFillRequest to populate next row with 10 columns(just a try). 尝试使用autoFillRequest用10列填充下一行(尝试一下)。 FAILED 失败

I've put in comment the getFormulas-like request and show you what I have for the moment with the autoFill request. 我已经在类似getFormulas的请求中添加了注释,并向您展示了autoFill请求目前所拥有的内容。

I get the following error: 我收到以下错误:

Invalid JSON payload received. 接收到无效的JSON有效负载。 Unknown name "source_and_destination" at 'requests[0]': Cannot find field. “ requests [0]”处的未知名称“ source_and_destination”:找不到字段。

function insertNewRow(){
  var ssId = "my_spreadsheet_id"
  /*var params = {
    ranges: ['Feuille1!21:21'],
    valueRenderOption: 'FORMULA'
  };
  var values = Sheets.Spreadsheets.Values.batchGet(ssId, params);
  var valueRange = Sheets.newValueRange();
  valueRange.majorDimension = "ROWS";
  valueRange.values = values.valueRanges[0].values;
  Logger.log(values.valueRanges[0].values[0].length);
  valueRange.range= 'Feuille1!22:22'
  //var req = Sheets.Spreadsheets.Values.update(valueRange , ssId, 'Feuille1!22:22', {
  //  valueInputOption: 'USER_ENTERED'
  //})*/
  var AFreq = Sheets.newAutoFillRequest();

  AFreq.range = Sheets.newGridRange();
  AFreq.range.startRowIndex = 1;
  AFreq.range.startColumnIndex = 0;
  AFreq.range.endRowIndex = 2;
  AFreq.range.endColumnIndex = 10;
  AFreq.range.sheetId = 0;

  AFreq.sourceAndDestination = Sheets.newSourceAndDestination();
  AFreq.sourceAndDestination.dimension = "ROWS";
  AFreq.sourceAndDestination.fillLength = 10;

  AFreq.sourceAndDestination.source = Sheets.newGridRange();
  AFreq.sourceAndDestination.source.startRowIndex = 0;
  AFreq.sourceAndDestination.source.startColumnIndex = 0;
  AFreq.sourceAndDestination.source.endColumnIndex = 10   
  AFreq.sourceAndDestination.source.endRowIndex = 1;
  AFreq.sourceAndDestination.source.sheetId = 0;

  var req = Sheets.newBatchUpdateSpreadsheetRequest();
  req.requests = [AFreq];
  Sheets.Spreadsheets.batchUpdate(req, ssId);
  }

Tell me if I'm wrong but I though about separating the tasks into multiple requests 告诉我是否有错,但是我可以将任务分成多个请求

  1. grab the formulas 抓住公式
  2. insert new row 插入新行
  3. copy/paste preceding fromat to new row 从fromat复制/粘贴到新行
  4. pasting formulas 粘贴公式

Am I going in the right direction? 我朝着正确的方向前进吗? Any help is greatly appreciated. 任何帮助是极大的赞赏。

Issues: 问题:

  • Request object is missing in Request body. 请求主体中缺少Request对象。
  • AutoFillRequest has two union area fields, whereas exactly one is acceptable. AutoFillRequest具有两个联合区域字段,而仅一个是可接受的。
  • Empty range selection in GridRange. GridRange中的空范围选择。

Solution: 解:

  • Fix syntax errors mentioned above 修复上述语法错误
  • Used plain JSON request body to easily identify such errors 使用普通的JSON请求正文轻松识别此类错误

Sample Script: 示例脚本:

function autoFill() {
  var ssId = 'my_spreadsheet_id';
  var req = {//request body    
    requests: [ //requests array    
      {//request Object
        autoFill: { //autoFill request
          //range OR sourceAndDestination;
          //equal to selecting Sheet1!A1:J10 and clicking autoFill from menu
          range: {//GridRange
            sheetId: 0,
            startRowIndex: 0,
            endRowIndex: 10, //end exclusive
            startColumnIndex: 0,
            endColumnIndex: 10,
          },
        },
      },
    ],
  };
  Sheets.Spreadsheets.batchUpdate(req, ssId);
}

References: 参考文献:

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

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