I've been using Google Sheets API, and following The Google Guide. However there is no example, even beyond the second page of google, to add a worksheet and write to a new sheet in .NET. There is plenty for js, but I have no idea on how to 1) add a sheet or 2) write to a new sheet.
How can I do this? Right now I'm able to read like in the example with out any problems, and I've only found one other reference to v4 C#. I tried going back to v3, but all documents strongly suggest using v4.
Has anyone been able to do this? Here's all I've been able to do so far:
// 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??
To save someone in the future a headache to end all headaches. I figured out how to add a sheet after hours of trial and error. Still working on how to update values.
Here's how I did it:
// 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();
I was looking for the Java version of this and I managed to get a working version based on sparky's answer. This should work:
//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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With