简体   繁体   English

Google Sheets API v4:添加命名或受保护的范围

[英]Google Sheets API v4: Add named or protected ranges

Can't get the Google Sheets API v4 code working from Google Apps Script for adding named/protected ranges to the Google Sheet, used the code sample from here [[link]][1]. 无法从Google Apps脚本获取用于将已命名/受保护范围添加到Google Sheet的Google Sheets API v4代码,请使用此处的代码示例[[link]] [1]。 It gives the error (what is the correct json form for this?): 它给出了错误(正确的json格式是什么?):

Invalid JSON payload received. 接收到无效的JSON有效负载。 Unknown name "requests": Cannot find field. 未知名称“ requests”:找不到字段。 (line 5, file "Code") (第5行,文件“代码”)

Below is the code: 下面是代码:

function protectSheet() {
    var sheetId = "sheet id";
    var spreadsheetId = "spreadsheet id";

    Sheets.Spreadsheets.Values.batchUpdate(
    {
        "requests": [
        {
            "addNamedRange": {
                "namedRange": {
                    "name": "Counts",
                    "range": {
                        "sheetId": sheetId,
                        "startRowIndex": 0,
                        "endRowIndex": 3,
                        "startColumnIndex": 0,
                        "endColumnIndex": 5,
                    },
                }
            }
        },
        {
            "addProtectedRange": {
                "protectedRange": {
                    "range": {
                        "sheetId": sheetId,
                        "startRowIndex": 3,
                        "endRowIndex": 4,
                        "startColumnIndex": 0,
                        "endColumnIndex": 5,
                    },
                    "description": "Protecting total row",
                    "warningOnly": true
                }
            }
        }
        ]
    }, spreadsheetId);
}


[1]: https://developers.google.com/sheets/api/samples/ranges

I think that your request body is correct. 我认为您的要求内容正确。 So how about modifying as follows? 那么如何进行如下修改?

From: 从:

Sheets.Spreadsheets.Values.batchUpdate(

To: 至:

Sheets.Spreadsheets.batchUpdate(

Reference: 参考:

If you have any issues for the request body, please tell me. 如果请求正文有任何问题,请告诉我。 I would like to think of the issues. 我想考虑这些问题。

Edit: 编辑:

Invalid value at 'requests[0].add_named_range.named_range.range.sheet_id' (TYPE_INT32), "sheet id" Invalid “ requests [0] .add_named_range.named_range.range.sheet_id”(TYPE_INT32)上的值无效,“工作表ID”无效

From your error message, it is found that you use sheet id as the sheet ID. 从错误消息中,发现您使用工作sheet id作为工作表ID。 So please modify the sheet ID from sheet id to the correct one. 因此,请将工作表ID从工作sheet id修改为正确的ID。

If you want to manually retrieve the sheet ID. 如果要手动获取工作表ID。 Please check here . 在这里检查。

If you want to retrieve the sheet ID using script, how about this? 如果要使用脚本检索工作表ID,该如何处理?

var spreadsheetId = "spreadsheet id"; // Please set spreadsheetId here.
var ss = SpreadsheetApp.openById(spreadsheetId);
var sheetId = ss.getSheetByName(sheetName).getSheetId(); // sheetName is the sheet name of each sheet.

or 要么

var sheetId = ss.getSheets()[index].getSheetId(); // index is the index of sheet. This is start from 0.

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

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