【问题标题】:Creating a new spreadsheet with default row and column values创建具有默认行和列值的新电子表格
【发布时间】:2019-09-11 16:04:22
【问题描述】:

我想创建一个具有默认行和列值的 Google 电子表格。我知道如何创建具有所需标题、默认格式等的电子表格,但不知道如何使用所需数据填充它。

到目前为止我所做的:

function makeApiCall() {
      var spreadsheetBody = {
        // TODO: Add desired properties to the request body.
        "properties": {
    "title": "Created from localhost"
  },

      };

      var request = gapi.client.sheets.spreadsheets.create({}, spreadsheetBody);
      request.then(function(response) {

        console.log(response.result);
      }, function(reason) {
        console.error('error: ' + reason.result.error.message);
      });
    }

我参考了 API 文档,似乎没有关于通过填充默认值来创建电子表格的信息。

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create?apix_params=%7B%22resource%22%3A%7B%7D%7D

【问题讨论】:

  • 您可以从另一个副本创建电子表格,其中包含您想要的所有标题和正确格式。您可以使用 range.setValues 或 sheet.appendRow() 或许多复制命令中的任何一个添加值。阅读文档的参考部分,它都在里面。
  • 您想使用不是 Google Apps 脚本的 Javascript 来实现您的目标。我的理解正确吗?

标签: javascript google-apps-script google-chrome-extension


【解决方案1】:

如果您的目标是使用 Javascript 中的 Sheets API 为电子表格设置值:

创建电子表格后,您可以检索其 ID 并使用 batchUpdate 方法通过 requests“addSheet”和“pasteData”根据需要添加工作表和内容。

示例请求:

var myData="1,2,3,, ,4";
var request={requests: [
              { 
               pasteData: {
                data: myData,
                type: "PASTE_NORMAL",
                delimiter: ",",
                coordinate: {
                  sheetId: 0,
                  rowIndex: 0,
                  }
                }
              }
           ]
        };

文档中的示例 batchUpdate:

    function makeApiCall() {
      var params = {
        // The spreadsheet to apply the updates to.
        spreadsheetId: 'my-spreadsheet-id',  // TODO: Update placeholder value.
      };

      var batchUpdateSpreadsheetRequestBody = {
        // A list of updates to apply to the spreadsheet.
        // Requests will be applied in the order they are specified.
        // If any request is not valid, no requests will be applied.
        requests: request,  // TODO: Update placeholder value.

        // TODO: Add desired properties to the request body.
      };

      var request = gapi.client.sheets.spreadsheets.batchUpdate(params, batchUpdateSpreadsheetRequestBody);
      request.then(function(response) {
        // TODO: Change code below to process the `response` object:
        console.log(response.result);
      }, function(reason) {
        console.error('error: ' + reason.result.error.message);
      });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多