【发布时间】: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