以编程方式编辑 Google 电子表格

2024-03-08

我编写了一个接受用户输入的程序,但现在我希望能够通过在每次用户提交表单时编辑 Google 电子表格来保存该输入。基本上,Google 电子表格会不断更新。

谁能提供有关我如何实现这一目标的教程?我正在使用 Eclipse 用 Ja​​va 进行编写,那么我需要哪些插件?

我已经尝试使用中提供的一些示例代码谷歌电子表格 API https://developers.google.com/google-apps/spreadsheets/ (添加列表行 https://developers.google.com/google-apps/spreadsheets/#adding_a_list_row部分),但我似乎无法让它工作。

import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.net.*;
import java.util.*;

public class MySpreadsheetIntegration {
  public static void main(String[] args)
      throws AuthenticationException, MalformedURLException, IOException, ServiceException {

    SpreadsheetService service =
        new SpreadsheetService("MySpreadsheetIntegration-v1");

    // TODO: Authorize the service object for a specific user (see other sections)

    // Define the URL to request.  This should never change.
    URL SPREADSHEET_FEED_URL = new URL(
        "https://docs.google.com/spreadsheets/d/1OcDp1IZ4iuvyhndtrZ3OOMHZNSEt7XTaaTrhEkNPnN4/edit#gid=0");

    // Make a request to the API and get all spreadsheets.
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
        SpreadsheetFeed.class);
    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

    if (spreadsheets.size() == 0) {
      // TODO: There were no spreadsheets, act accordingly.
    }

    // TODO: Choose a spreadsheet more intelligently based on your
    // app's needs.
    SpreadsheetEntry spreadsheet = spreadsheets.get(0);
    System.out.println(spreadsheet.getTitle().getPlainText());

    // Get the first worksheet of the first spreadsheet.
    // TODO: Choose a worksheet more intelligently based on your
    // app's needs.
    WorksheetFeed worksheetFeed = service.getFeed(
        spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
    List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
    WorksheetEntry worksheet = worksheets.get(0);

    // Fetch the list feed of the worksheet.
    URL listFeedUrl = worksheet.getListFeedUrl();
    ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);

    // Create a local representation of the new row.
    ListEntry row = new ListEntry();
    row.getCustomElements().setValueLocal("firstname", "Joe");
    row.getCustomElements().setValueLocal("lastname", "Smith");
    row.getCustomElements().setValueLocal("age", "26");
    row.getCustomElements().setValueLocal("height", "176");

    // Send the new row to the API for insertion.
    row = service.insert(listFeedUrl, row);

  }
}

似乎很晚了,但这肯定会帮助其他人!问题出在您的 SPREADSHEET_FEED_URL 和 SpreadSheetService 实例的身份验证中,因为官方电子表格 API https://developers.google.com/google-apps/spreadsheets/?csw=1尚未分享对此的详细解释。您需要获取身份验证令牌并将其设置在 SpreadSheetService 实例上,如下所示才能使其工作:

 private void getAuthenticationToken(Activity activity, String accountName){
            //Scopes used to get access to google docs and spreadsheets present in the drive
            String SCOPE1 = "https://spreadsheets.google.com/feeds";
            String SCOPE2 = "https://docs.google.com/feeds";
            String scope = "oauth2:" + SCOPE1 + " " + SCOPE2;
            String authenticationToken = null;
            try {
                accessToken= GoogleAuthUtil.getToken(activity, accountName, scope);
            }
            catch (UserRecoverableAuthException exception){
    //For first time, user has to give this permission explicitly
                Intent recoveryIntent = exception.getIntent();
                    startActivityForResult(recoveryIntent, RECOVERY_REQUEST_CODE);
            }catch (IOException e) {
                e.printStackTrace();
            } catch (GoogleAuthException e) {
                e.printStackTrace();
            }        
        }

         @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
               if (requestCode == RECOVERY_REQUEST_CODE){
                    if(resultCode == RESULT_OK){
                        if(data != null){
                            String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                            if (accountName != null && !accountName.equals("")){
    //To be called only for the first time after the permission is given
                                getAuthenticationToken(activity, accountName);
                            }
                        }else {
                            Utility.showSnackBar(linearLayout, Constants.INTENT_DATA_NULL);
                        }
                    }
                }
            }

最后,下面的代码用于获取电子邮件帐户中的所有电子表格:

public class MySpreadsheetIntegration {
  public void getSpreadSheetEntries()
      throws AuthenticationException, MalformedURLException, IOException, ServiceException {

    SpreadsheetService service =
        new SpreadsheetService("MySpreadsheetIntegration-v1");
 service = new SpreadsheetService(applicationName);
             service .setProtocolVersion(SpreadsheetService.Versions.V3);
    service .setAuthSubToken(accessToken);

    // Define the URL to request.  This should never change.
    URL SPREADSHEET_FEED_URL = new URL(
        "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

    // Make a request to the API and get all spreadsheets.
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

    // Iterate through all of the spreadsheets returned
    for (SpreadsheetEntry spreadsheet : spreadsheets) {
      // Print the title of this spreadsheet to the screen
      System.out.println(spreadsheet.getTitle().getPlainText());
    }
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

以编程方式编辑 Google 电子表格 的相关文章

随机推荐

  • 从 Java 写入 XML 文档 - 简单

    我知道 stackoverflow 上有很多关于从 Java 编写到 XML 的问题 但它太复杂了 我觉得我有一个非常简单的问题 但我无法弄清楚 因此 我有一个程序需要大量用户输入 并且当前正在创建并附加带有结果的文本文档 我将在这里发布我
  • 具有附加可绑定字段的 ASP.NET Server 控件

    我创建了一个自定义服务器控件 源自System Web Contols CheckBoxList定制如何CheckBoxList被渲染 我还想添加另一个可绑定字段并获取该字段的值CheckBoxList RenderItem 方法 我想要创
  • 构建时 PNG 生成不支持对其他资源的引用

    AndroidStudio 3 0 Android Gradle 插件 3 0
  • TableLayout中表行的索引

    我有一个 TableLayout 和一些未知数量的 TableRows 它们是根据数据库中的内容生成的 每行都附加了一个 OnClick 侦听器 但是 一旦发生单击 我就无法 有意义地 分辨它来自哪一行 有没有一种方法可以获取与 Table
  • lib 依赖项及其顺序

    有时 如果我们没有在 makefile 中按特定顺序列出库 则会失败 原因是 定义应该先于其使用 如何确定正确的顺序 实际上 在链接库时 使用应该在定义之前 在处理提供其定义的库文件之前 需要知道任何未解析的符号 恐怕您必须手动执行此命令
  • Facebook Graph API 返回空数据集

    我正在尝试使用 Graph API Explorer 为我的应用程序创建访问令牌 以使用 me accounts 查看我的页面 但是 每次我尝试此操作时 它都会返回一个空数据集 我已经选择了manage pages作为权限 但它仍然不起作用
  • 如何生成具有特定概率密度函数的随机数?

    我正在尝试对移动无线网络的阴影和快速衰落进行建模 对于快速衰落 瑞利衰落是一个合理的模型 信道响应的包络将是瑞利分布的 将该随机变量称为 R 其概率密度函数 PDF 为 PR r 2r exp r 2 r gt 0 2 2 http en
  • Hudson 和 Maven 的双单元测试报告

    我在 hudson 有一个 maven2 项目 当 cobertura 报告插件运行时 它会导致单元测试显示它们已经运行了两次 我不介意它们运行多次 但趋势图显示的测试数量是我们实际运行的两倍 有没有办法确保图表只显示一次 thanks J
  • Oracle 中的截断表出现错误

    我遇到的问题是 当我在 Oracle 中运行以下命令时 遇到错误 Truncate table mytable Errors ORA 02266 unique primary keys in table referenced by enab
  • 眼镜检测

    我想做的是测量眼镜框的厚度 我有一个想法来测量框架轮廓的厚度 可能是更好的方法 到目前为止 我已经勾勒出眼镜框的轮廓 但线条不相交处存在间隙 我考虑过使用 HoughLinesP 但我不确定这是否是我需要的 到目前为止 我已执行以下步骤 将
  • 来自私有 GitHub 存储库的 Cordova 插件

    从私有 GitHub 存储库安装 Cordova 插件的首选方法是什么 我从供应商处购买了一个私有插件 该供应商授予我的 git 帐户访问其私有 git 存储库的权限 通常我从 cordova cli 安装插件 cordova plugin
  • 如何比较两个数组,删除相似的项目,而不迭代整个数组?

    是否可以比较两个数组并删除相等的值 如果它们位于相同的索引 而不需要迭代两个数组 这是一个例子 array1 1 2 3 4 5 6 7 23 44 array2 1 1 3 4 5 7 6 23 45 array3 sudo compar
  • 提供 if 语句问题的函数返回

    我在从 golang 的 if 语句中返回函数的预期返回语句时遇到问题 我提供了以下代码 package main import fmt func random string var x return if x return return
  • 生命周期困境与另一项活动的方向变化

    我在 tabhost 中有 2 项活动 在 Activity1 中 我处理方向变化以及用户在 Activity 之间切换时的情况 当用户从 Activity1 切换到 Activity2 通过选项卡选择 执行方向更改 然后切换回 Activ
  • 限制 python 程序的 RAM 使用

    我试图将 Python 程序的 RAM 使用量限制为一半 这样当使用所有 RAM 时它就不会完全冻结 为此我使用了以下代码 该代码不起作用 并且我的笔记本电脑仍然冻结 import sys import resource def memor
  • Haskell 中的“子类化”显示?

    可以说我有以下内容 data Greek Alpha Beta Gamma Phi deriving Show 我想使用除 Beta 之外的所有项目的默认显示 我想说 两个 我可以这样做吗 deriving Show使用标准实例化机制 简单
  • 如何在 Safari 中打开 url 并返回到 Xcode 7 中 UITests 下的应用程序?

    这是我的自定义视图 LondonStreet 是一个按钮 当我点击该按钮时 我会获取 url 并在 Safari 中打开它 它有效 然后我可以返回 使用 Back to Wishlist 按钮 它也有效 问题是当我尝试在 UITests 下
  • 如何设置 select2 下拉列表的最小宽度和最大宽度?

    我在响应式 div 中有一个 select2 下拉菜单 该 div 还有一个侧边栏元素 列 其中包含用户从下拉列表中进行的选择 他们可以选择一个选项并将其添加到侧边栏 一切正常 但我在下拉菜单中有一个很长的选项 如果用户选择此选项 则父 d
  • Woocommerce Checkout:在国家/地区下拉列表中添加占位符[重复]

    这个问题在这里已经有答案了 在我的 Woocommerce Shop 结账处有一个下拉菜单 可以选择您所在的国家 地区 默认情况下 美国已被自动选择 如何只使用 选择您所在国家 地区 的占位符 我找不到这个主题的任何解决方案有人有什么想法吗
  • 以编程方式编辑 Google 电子表格

    我编写了一个接受用户输入的程序 但现在我希望能够通过在每次用户提交表单时编辑 Google 电子表格来保存该输入 基本上 Google 电子表格会不断更新 谁能提供有关我如何实现这一目标的教程 我正在使用 Eclipse 用 Ja va 进