Search code examples
javaspreadsheetgdata

Java Google Spreadsheet API: Updating Public Worksheet 'java.lang.UnsupportedOperationException: Entry cannot be updated'


I am quite new to Java and new to the Google Data API's. I am trying to change the size of a worksheet in a publicly shared spreadsheet. I'm using the following code:

import com.google.gdata.client.authn.oauth.*;
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.*;
import com.google.gdata.data.batch.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.net.*;
import java.util.*;
static void writeToGoogleSpreadsheet(String spreadsheetKey) throws IOException, ServiceException {
        SpreadsheetService service = new SpreadsheetService("com.example");
        String urlString = "https://spreadsheets.google.com/feeds/worksheets/" + spreadsheetKey + "/public/basic";

       URL url = new URL(urlString);

    try {
      WorksheetFeed worksheetFeed = service.getFeed(url, WorksheetFeed.class);
        List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
        WorksheetEntry worksheet = worksheets.get(0);

        System.out.println(worksheet.getTitle().getPlainText());
        System.out.println(worksheet.getCanEdit());

        // Update the local representation of the worksheet.
        worksheet.setTitle(new PlainTextConstruct("Updated Worksheet"));
        worksheet.setColCount(40);
        worksheet.setRowCount(40);

        // Send the local representation of the worksheet to the API for
        // modification.
        worksheet.update();
   } finally{}
}

The console displays the correct worksheet title and size, so I'm pretty sure I am accessing the right worksheet. However, worksheet.update() throws the following exception:

Exception in thread "main" java.lang.UnsupportedOperationException: Entry cannot be updated
at com.google.gdata.data.BaseEntry.update(BaseEntry.java:635)
atcom.example.GoogleSpreadsheetCommunicator.writeToGoogleSpreadsheet(GoogleSpreadsheetCommunicator.java:119)
at com.example.Main.guildSim(Main.java:114)
at com.example.Main.main(Main.java:73)

Does anyone know what I am doing wrong?

Thanks for reading and kind regards,

Karel


Solution

  • You can't edit a public feed, change urlString to use .../private/...

    and you will need to use one of the 3 ways to authenticate you can find on https://developers.google.com/google-apps/spreadsheets/#authorizing_requests_with_clientlogin