【问题标题】:Adding note to cell in google sheets c#在谷歌表格中添加注释到单元格c#
【发布时间】:2020-06-30 14:57:10
【问题描述】:

是否可以使用 Google 向单元格添加注释? APIs.Sheets.v4?

在 python 中似乎是可能的:Is it possible to use the Google Spreadsheet API to add a comment in a cell? 但我没有成功将它复制到 c#,Insert a comment in a Google Sheet with google-sheets-api 描述了如何使用 https 调用添加它,但我更愿意使用 google c# 库而不是开始发送如果可能,HTTP 会调用我自己。 如果有人知道如何做,或者可以将我指向一个工作示例,那将是一个巨大的帮助。

提前致谢。

根据 Rafa Guillermos 的建议,我成功了。

        public async void AddNote(string sheet, int column, int row, int sheetId, string noteMessage)
        {
            await Task.Delay(1);

            var requests = new List<Request>();


            // Grid range for a single cell, end column, and row have to be +1, otherwise, sheet throws error trying to write outside bounds.
            var gridRange = new Google.Apis.Sheets.v4.Data.GridRange
            {
                EndColumnIndex = column + 1, StartColumnIndex = column, EndRowIndex = row + 1, StartRowIndex = row, SheetId = sheetId
            };

            // Building a request for update cells.
            var request = new Google.Apis.Sheets.v4.Data.Request();
            request.UpdateCells = new Google.Apis.Sheets.v4.Data.UpdateCellsRequest();
            request.UpdateCells.Range = gridRange;
            request.UpdateCells.Fields = "note";
            request.UpdateCells.Rows = new List<Google.Apis.Sheets.v4.Data.RowData>();
            request.UpdateCells.Rows.Add(new Google.Apis.Sheets.v4.Data.RowData());
            request.UpdateCells.Rows[0].Values = new List<Google.Apis.Sheets.v4.Data.CellData>();
            request.UpdateCells.Rows[0].Values.Add(new Google.Apis.Sheets.v4.Data.CellData());
            request.UpdateCells.Rows[0].Values[0].Note = noteMessage;

            requests.Add(request);

            var requestBody = new Google.Apis.Sheets.v4.Data.BatchUpdateSpreadsheetRequest();
            requestBody.Requests = requests;

            var service = _authenticatorService.GetSheetsService(new[] { SheetsService.Scope.Spreadsheets} );

            var batchRequest = service.Spreadsheets.BatchUpdate(requestBody, _spreadsheetId);
            batchRequest.Execute();
        }

_authenticatorService 提供了一个经过身份验证的工作表服务。

【问题讨论】:

  • 如果用户在 stackoverflow 上为您提供了有用的答案,正确的过程是将答案 (✓) 标记为已接受,而不是用用户的答案替换您的原始问题。这将有助于其他有类似问题的人了解他们应该遵循哪些步骤来找到解决方案。

标签: c# google-sheets-api


【解决方案1】:

答案:

与 python 完全相同,您需要在 C# 中将便笺构建为批处理请求。

代码片段:

您需要将数据请求构建为如下列表:

List<Data.Request> requests = new List<Data.Request>();

并将值分配给批处理的请求正文:

Data.BatchUpdateSpreadsheetRequest requestBody = new Data.BatchUpdateSpreadsheetRequest();
requestBody.Requests = requests;

在构建请求对象之前:

SpreadsheetsResource.BatchUpdateRequest request = sheetsService.Spreadsheets.BatchUpdate(requestBody, spreadsheetId);

并执行请求:

Data.BatchUpdateSpreadsheetResponse response = request.Execute();

更多信息:

您可以阅读有关spreadsheets.batchUpdate here 以及页面底部here 的C# 示例代码。

可以在here 中找到请求资源的 JSON 表示形式,其结构与 answer you linked here 相同。

参考资料:

【讨论】:

  • 感谢您的快速回复 :) 您在代码中的哪个位置添加了注释?我使用 api v4,您是否使用其他版本可以向 ValueRange 添加注释?还有你的 valueInputOption 是什么?这是一个字符串,所以很高兴知道你使用了什么。
  • 您需要以与请求文档中所需结构的方式相同的形式提出请求List object。你看过我提供的链接吗? Google 提供的 .NET 示例可以很好地引导您完成整个过程。
  • 我在您发送解决方案之前和之后都阅读过它们。您使用 Data.BatchUpdateValuesRequest requestBody = new Data.BatchUpdateValuesRequest();在您的回复中,它是否意味着新的 BatchUpdateSpreadsheetRequest()? sheetService.Spreadsheets.BatchUpdate 不接受前者。如果不是,并且您打算改用 .Spreadsheets.Values.BatchUpdate,那么您使用什么 ValueInputOption?
  • 你是对的,我很抱歉我贴错了行。我现在已经解决了。您需要使用List.Add 构建requests,您添加的每个对象都是Data.Request object
  • 感谢您的帮助,让它工作。由于这是我的第一篇文章,因此无法为您的解决方案投票,但我会用该解决方案更新问题,并希望其他人可以使用它并为您的回复投票。
猜你喜欢
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
相关资源
最近更新 更多