使用 google 电子表格创建电子表格时将 header 添加到电子表格 CREATE API

Add header to spreadsheet while creating spreadsheet using google spreadsheet CREATE API

我正在使用 google 电子表格 API 使用 node.js 创建电子表格。我想在创建电子表格时添加一个 header(具有三列 h1、h2、h3)。我可以创建电子表格,但无法添加 header。有什么选择吗?

下面的GAS样本怎么样?如果你想运行除了GAS,请告诉我。

为了使用它,首先,请启用 Google Sheet API v4 高级 Google 服务和 Google API控制台。

使用方法如下

  1. 在脚本编辑器中,select资源>高级Google服务

  2. 在出现的对话框中,点击 on/off 开关 Google Sheets API.

  3. 在对话框底部,单击 Google API 控制台的 link。

  4. 在控制台中,单击筛选框并键入 API "Google Sheets API" 的部分名称,然后在看到名称后单击它。

  5. 在下一个屏幕上,单击“启用”API。

  6. 关闭开发者控制台并return到脚本编辑器。在对话框中单击“确定”。您启用的高级服务现在可以自动完成。

详细信息为https://developers.google.com/apps-script/guides/services/advanced

脚本:

Sheets.Spreadsheets.create({
  "properties": 
  {
    "title": "filename" // filename
  },
  "sheets": 
  [
    {
      "data": 
      [
        {
          "startRow": 0, // 1st row
          "startColumn": 7, // column h
          "rowData": 
          [
            {
              "values": 
              [
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "sample text h1"
                  }
                }
              ]
            },
            {
              "values": 
              [
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "sample text h2"
                  }
                }
              ]
            },
            {
              "values": 
              [
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "sample text h3"
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
});

结果:

抱歉 JSON 数据太长了。请更改示例文本。如果要为单元格使用数字,请使用 "numberValue" 而不是 "stringValue"

如果我误解了你的问题,我很抱歉。

添加了 1 个:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange('h1:h3');
var protection = range.protect().setDescription('protected');  
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

以上脚本保护h1:h3。所有者可以编辑所有单元格,但其他用户只能编辑 h1:h3

添加了 2 个:

Sheets.Spreadsheets.create({
  "properties": 
  {
    "title": "filename"
  },
  "sheets": 
  [
    {
      "protectedRanges": 
      [
        {
          "range": 
          {
            "startColumnIndex": 7,
            "endColumnIndex": 8,
            "startRowIndex": 0,
            "endRowIndex": 3,
            "sheetId": 0
          },
          "description": "protected",
          "editors": 
          {
            "users": 
            ["your e-mail address"
            ]
          }
        }
      ],
      "data": 
      [
        {
          "startColumn": 7,
          "startRow": 0,
          "rowData": 
          [
            {
              "values": 
              [
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "sample text h1"
                  }
                }
              ]
            },
            {
              "values": 
              [
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "sample text h2"
                  }
                }
              ]
            },
            {
              "values": 
              [
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "sample text h3"
                  }
                }
              ]
            }
          ]
        }
      ],
      "properties": 
      {
        "sheetId": 0
      }
    }
  ]
});

此脚本创建新电子表格。那时,它将数据添加到 'h1:h3' 并保护它们。请添加您的电子邮件地址作为编辑。这个是用GoogleSheetAPIv4.