【问题标题】:Add header to spreadsheet while creating spreadsheet using google spreadsheet CREATE API在使用谷歌电子表格 CREATE API 创建电子表格时向电子表格添加标题
【发布时间】:2017-04-18 07:10:48
【问题描述】:

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

【问题讨论】:

    标签: api google-sheets header add google-spreadsheet-api


    【解决方案1】:

    下面的 GAS 样本怎么样?如果你想运行 GAS 以外的东西,请告诉我。

    若要使用此功能,请先启用 Advanced Google services 和 Google API Console 的 Google Sheet API v4。

    使用方法如下。

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

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

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

    4. 在控制台中,点击过滤器框并输入 API 的部分名称“Google Sheets API”,然后在看到名称后点击该名称。

    5. 在下一个屏幕上,点击启用 API。

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

    详细信息为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' 并保护它们。请添加您的电子邮件地址作为编辑。这是使用的 Google Sheet API v4。

    【讨论】:

    • 真的很有帮助!非常感谢。您能否也告诉我如何在创建工作表时使这三个值 H1、H2、H3 不可编辑?
    • 您可以使用类保护。您可以在此处查看详细信息。 developers.google.com/apps-script/reference/spreadsheet/…
    • 我认为这个参考对我有帮助,但我无法找到这个 SpreadsheetApp 类的来源以及如何将它集成到上面的代码中。你能帮帮我吗?
    • 更新了我的答案。请检查一下。通过添加的脚本,除了所有者之外的用户不能只编辑h1:h3。
    • 我的问题是 SpreadsheetApp 类是从哪里来的??以及将您添加到我的代码中的代码放在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多