How to add a sheet in google sheets API v4 in C#?

前端 未结 5 1694
無奈伤痛
無奈伤痛 2021-02-10 21:28

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 she

5条回答
  •  名媛妹妹
    2021-02-10 22:19

    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 requestsList = new ArrayList();
                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();
    

提交回复
热议问题