如何使用 google sheets api v4 重命名 sheet?

How to rename sheet using google sheets api v4?

我有一个代码,它的目的是重命名一个特定的 sheet,但是当执行 BatchUpdate 并且代码崩溃时,有没有人有任何想法?

        public void UpdateSheetName(string sheetName,string newSheetName)
    {
        //get sheet id by sheet name
        Spreadsheet spr = service.Spreadsheets.Get(SpreadsheetId).Execute();
        Sheet sh = spr.Sheets.Where(s => s.Properties.Title == sheetName).FirstOrDefault();
        int sheetId = (int)sh.Properties.SheetId;
        BatchUpdateSpreadsheetRequest bussr = new BatchUpdateSpreadsheetRequest();

        var request = new Request()
        {
              UpdateSpreadsheetProperties= new UpdateSpreadsheetPropertiesRequest(){
                   Properties=new SpreadsheetProperties()
                   {                      
                       Title= newSheetName,

                   },
                  Fields ="title"
               }

        };

        bussr.Requests = new List<Request>();
        bussr.Requests.Add(request);
        var bur = service.Spreadsheets.BatchUpdate(bussr, SpreadsheetId);
        bur.Execute();
    }

错误信息: 'requests[0]' (oneof) 处的值无效,oneof 字段 'kind' 已设置。无法设置 'updateSpreadsheetProperties' [400]

我找到问题了,问题是我用错了class,用UpdateSpreadsheetProperties代替UpdateSheetPropertiesRequest

        var request = new Request()
        {
            UpdateSheetProperties =new UpdateSheetPropertiesRequest {
            Properties=new SheetProperties()
            {
                Title=newName,
                SheetId=sheetId

            },
                Fields = "Title"
            }


        };