2

Besides the QuickStart tutorial - There is almost no examples of Google Sheets v4 with Java.

I'm trying to clear all the cells of a sheet using batchUpdate.

I read I need to add no values in order for it to remove all cells but it does nothing:

public void clearSheet(String sheetName) {
    try {
        // Build a new authorized API client service.
        Sheets service = GoogleApi.getInstance().getSheetsService();

        String spreadsheetId = GoogleSheetsSettings.getDataFileId();            

        BatchUpdateValuesRequest oRequest = new BatchUpdateValuesRequest();
        oRequest.setValueInputOption("RAW");
        //oRequest.setData(oList);
        BatchUpdateValuesResponse oResp1 = service.spreadsheets().values().batchUpdate(spreadsheetId, oRequest).execute();
    } catch (IOException ex) {
        Logger.getLogger(GoogleApi.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Could not execute spreadsheet update");
    }        
}

What is the right approach?

Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154
  • Try to check this [`Method: spreadsheets.values.clear`](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear) if it can help you. It Clears values from a spreadsheet. The caller must specify the spreadsheet ID and range. Only values are cleared -- all other properties of the cell (such as formatting, data validation, etc..) are kept. Check this [SO question](http://stackoverflow.com/questions/37928947/) for more information. – KENdi Dec 20 '16 at 09:31

1 Answers1

3

The working one is:

service.spreadsheets().values().clear(spreadsheetId, range, new ClearValuesRequest()).execute()
Sabfir
  • 186
  • 4
  • 5