【问题标题】:How to get range of google spread sheet dynamically如何动态获取谷歌电子表格的范围
【发布时间】:2020-02-03 11:33:28
【问题描述】:

我有一个谷歌电子表格,我必须打印每个单元格的所有数据。 我编写了一个获取数据的代码,但为此,我必须定义 "range" 的值。我希望范围是动态的,即无论谷歌电子表格的范围是什么。

public static void readSampleSpreadsheet() throws IOException {
    // Build a new authorized API client service.
    Sheets service = getSheetsService();
    String spreadsheetId = "1gfLQTGWejFeZmp_HpTVP84ykKkKZbAE";

    String range = "A1:AZ70";

    ValueRange response = service.spreadsheets().values()

            .get(spreadsheetId, range)
            .execute();


    List<List<Object>> values = response.getValues();
    System.out.println("this is the values  " + values);
    if (values == null || values.size() == 0) {
        System.out.println("No data found.");
    } else {
        System.out.println("Name, Major");
        for (List row : values) {
            // Print columns A and E, which correspond to indices 0 and 4.
            System.out.println(row);
        }
    }
}

【问题讨论】:

    标签: java google-sheets-api


    【解决方案1】:

    Sheets API 不提供此类功能

    相反,您可以使用具有 sheet.getDataRange() 方法的 Apps 脚本。

    您可以使用 Apps Script API 将 Apps 脚本代码嵌入到您的 java 代码中,这允许您使用 execute Apps Script functions remotely

    或者,您可以将 Apps 脚本代码部署为 WebApp,并将其返回值与部署 URL 合并到您的代码中,有关示例,请参阅 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2013-11-13
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多