'How can I insert a comment with the Sheets API?

I am using the Google Sheets API to to read spreadsheets and operate on the data. My application has limited permissions on the sheet it's operating on. I can not edit cell values, only add comments.

I want to use my application to write comments on specific cells (e.g M64 or A1), but I cannot figure out how to do it. I saw Insert a comment in a Google Sheet with google-sheets-api, but this thread does not present a way to insert comments with Java.

I attempted the following, which I found on Java Google SpreadSheet API, how to update cell to add a note?. It executed with no errors, but the cell never obtained a comment:

Request request = new Request();
request.setRepeatCell(
        new RepeatCellRequest()
        .setCell(
                new CellData()
                .setNote("test"))
        .setRange(
                new GridRange()
                .setSheetId(1)  // Sheet 1
                .setStartRowIndex(0)
                .setEndRowIndex(13)     //
                .setStartColumnIndex(0) // Cell M64
                .setEndColumnIndex(64)  //
        )
        .setFields("note")
        );
BatchUpdateSpreadsheetRequest batch = new BatchUpdateSpreadsheetRequest();
batch.setRequests(Arrays.asList(new Request[] {request}));
service.spreadsheets().batchUpdate(spreadsheetId, batch);

Does anyone know how to use the Google API Client Library for Java and the Sheets API to add a comment to a spreadsheet?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source