【问题标题】:Java Google Spreadsheet API: Updating Public Worksheet 'java.lang.UnsupportedOperationException: Entry cannot be updated'Java Google 电子表格 API:更新公共工作表“java.lang.UnsupportedOperationException:无法更新条目”
【发布时间】:2014-06-16 09:05:52
【问题描述】:

我对 Java 很陌生,对 Google 数据 API 也很陌生。我正在尝试更改公开共享电子表格中工作表的大小。我正在使用以下代码:

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{}
}

控制台显示正确的工作表标题和大小,所以我很确定我访问的是正确的工作表。但是,worksheet.update() 会抛出以下异常:

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)

有谁知道我做错了什么?

感谢您的阅读和亲切的问候,

卡雷尔

【问题讨论】:

    标签: java spreadsheet gdata


    【解决方案1】:

    您无法编辑公共供稿, 将urlString 改为使用.../private/...

    您需要使用https://developers.google.com/google-apps/spreadsheets/#authorizing_requests_with_clientlogin 上提供的 3 种身份验证方式之一

    【讨论】:

    • 这可以解释很多!谢谢!您是否有相关文档供将来参考?因为developers.google.com/google-apps/spreadsheets/…'注意:单元提要支持私人和公共可见性,以及完整和基本的预测。建议可以编辑公共信息流。
    • np!我从经验中知道这一点,遇到了同样的问题,所以我查看了 BaseEntry.java 的源代码。另外请将答案标记为已接受,以便对其他人有所帮助
    猜你喜欢
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多