[英].Net (C#) - How to rename list (sheet) using google sheets api v4?
[英]How to add a sheet in google sheets API v4 in C#?
我一直在使用Google表格API,并遵循Google指南 。 然而,除了谷歌的第二页之外,没有任何例子可以添加工作表并在.NET中写入新工作表。 有很多js,但我不知道如何1)添加一张或2)写入新表。
我怎样才能做到这一点? 现在我能像在例如与出任何问题阅读,我只找到一个其他的参考V4 C#。 我尝试回到v3,但所有文档强烈建议使用v4。
有没有人能够做到这一点? 以下是我迄今为止所做的一切:
// Create Google Sheets API service.
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define request parameters.
// Add new Sheet
string sheetName = string.Format("{0} {1}", DateTime.Now.Month, DateTime.Now.Day);
AddSheetRequest addSheetRequest = new AddSheetRequest();
addSheetRequest.Properties.Title = sheetName;
// How do I tell this to update??
为了挽救将来的某个人头痛,以结束所有的头痛。 我想出了如何在经过数小时的试验和错误后添加工作表。 仍在研究如何更新值。
我是这样做的:
// Create Google Sheets API service.
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Add new Sheet
string sheetName = string.Format("{0} {1}", DateTime.Now.Month, DateTime.Now.Day);
var addSheetRequest = new AddSheetRequest();
addSheetRequest.Properties = new SheetProperties();
addSheetRequest.Properties.Title = sheetName;
BatchUpdateSpreadsheetRequest batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest();
batchUpdateSpreadsheetRequest.Requests = new List<Request>();
batchUpdateSpreadsheetRequest.Requests.Add(new Request
{
AddSheet = addSheetRequest
});
var batchUpdateRequest =
service.Spreadsheets.BatchUpdate(batchUpdateSpreadsheetRequest, spreadsheetId);
batchUpdateRequest.Execute();
我正在寻找这个Java版本,我设法得到一个基于sparky的答案的工作版本。 这应该工作:
//Set sheet name
//Can be any string, I chose to set it to the account name
String sheetName = mCredential.getSelectedAccountName();
//Create a new AddSheetRequest
AddSheetRequest addSheetRequest = new AddSheetRequest();
SheetProperties sheetProperties = new SheetProperties();
//Add the sheetName to the sheetProperties
addSheetRequest.setProperties(sheetProperties);
addSheetRequest.setProperties(sheetProperties.setTitle(sheetName));
//Create batchUpdateSpreadsheetRequest
BatchUpdateSpreadsheetRequest batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest();
//Create requestList and set it on the batchUpdateSpreadsheetRequest
List<Request> requestsList = new ArrayList<Request>();
batchUpdateSpreadsheetRequest.setRequests(requestsList);
//Create a new request with containing the addSheetRequest and add it to the requestList
Request request = new Request();
request.setAddSheet(addSheetRequest);
requestsList.add(request);
//Add the requestList to the batchUpdateSpreadsheetRequest
batchUpdateSpreadsheetRequest.setRequests(requestsList);
//Call the sheets API to execute the batchUpdate
mService.spreadsheets().batchUpdate(spreadsheetId,batchUpdateSpreadsheetRequest).execute();
我有代码添加一张表,但似乎你想出来了。 以下是一些向内容添加内容的代码。 它与其他人发布的内容有所不同,但它应该有助于看到这篇文章的人。
在此代码中,它会将colNames的值插入从(0,0)开始的行。 因此,如果sheet使用这种表示法(row,col),那么
(0,0)=时间戳
(0,1)= VideoID的
(0,2)= videoname
(0,3)=名字
(0,4)=姓
(0,5)=电子邮件
因此整个第一行将填充colNames值
var reqs = new BatchUpdateSpreadsheetRequest();
reqs.Requests = new List<Request>();
string[] colNames = new [] { "timestamp", "videoid", "videoname", "firstname", "lastname", "email" };
// Create starting coordinate where data would be written to
GridCoordinate gc = new GridCoordinate();
gc.ColumnIndex = 0;
gc.RowIndex = 0;
gc.SheetId = SHEETID; // Your specific sheet ID here
rq = new Request();
rq.UpdateCells = new UpdateCellsRequest();
rq.UpdateCells.Start = gc;
rq.UpdateCells.Fields = "*"; // needed by API, throws error if null
// Assigning data to cells
RowData rd = new RowData();
List<CellData> lcd = new List<CellData>();
foreach (String column in colNames)
{
ExtendedValue ev = new ExtendedValue();
ev.StringValue = column;
CellData cd = new CellData();
cd.UserEnteredValue = ev;
lcd.Add(cd);
}
rd.Values = lcd;
// Put cell data into a row
List<RowData> lrd = new List<RowData>();
lrd.Add(rd);
rq.UpdateCells.Rows = lrd;
// It's a batch request so you can create more than one request and send them all in one batch. Just use reqs.Requests.Add() to add additional requests for the same spreadsheet
reqs.Requests.Add(rq);
// Execute request
response = sheetsService.Spreadsheets.BatchUpdate(reqs, Spreadsheet.SpreadsheetId).Execute(); // Replace Spreadsheet.SpreadsheetId with your recently created spreadsheet ID
仅供参考,虽然大多数文档只使用原始JSON,但它应该以1:1的比例映射到每种语言的构造。
例如,要更新值,您可能会执行以下操作:
var valueRange = new ValueRange(); valueRange.values = { { 1, 2, 3 }, { 4, 5, 6 } }; var range = "A1"; var update = service.Spreadsheets.Values.Update(valueRange, spreadsheetId, range); var result = update.execute();
以上是基于您的代码示例和参考文档@ https://developers.google.com/sheets/reference/rest/v4/spreadsheets.values/update的伪代码。
进一步扩展了Sam的答案。 以下内容让我更新一个单元格。
ValueRange VRx = new ValueRange();
IList<IList<object>> xx = new List<IList<object>>();
xx.Add(new List<object> { "test" });
VRx.Values = xx;
SpreadsheetsResource.ValuesResource.UpdateRequest update = service.Spreadsheets.Values.Update(VRx, spreadsheetId, "back!A19");
update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
UpdateValuesResponse result = update.Execute();
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.