写入Google表格/ Google表格 - Java

时间:2017-03-12 23:19:14

标签: java google-drive-api httprequest google-form

有没有办法将文字写入现有的共享Google文档,或使用Java在共享的Google文件夹中创建新的google文件(并且无需连接任何帐户,因为共享文档和文件夹)?

谢谢!

*修改

我创建了一个Google表单,而不是使用Google API,该表单由应用程序填充,并自动导出到Google表格。我可以执行“POST”Http请求或打开预先填好的Google表单:

try{URL url = new URL(my_google_form_direct_url);

    //PREPARE PARAMS
    Map<String,Object> params = new LinkedHashMap<>();
    params.put("entry." + id_1, "TEXT1");
    params.put("entry." + id_2, "TEXT2");
    StringBuilder postData = new StringBuilder();
    for(Map.Entry<String,Object> param : params.entrySet()){
        if(postData.length() != 0){postData.append('&');}
        postData.append(URLEncoder.encode(param.getKey(), StandardCharsets.UTF_8.name()));
        postData.append('=');
        postData.append(URLEncoder.encode(String.valueOf(param.getValue()), StandardCharsets.UTF_8.name()));}
    byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8.name());

/************************************/        
    //SEND POST
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
    conn.setDoOutput(true);
    conn.getOutputStream().write(postDataBytes);

    //GET RESPONSE
    int response = conn.getResponseCode();
    if(response == HttpURLConnection.HTTP_OK) {
        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8.name()));
        InputStream in = conn.getInputStream();
        in.close();}
    conn.disconnect();

/************************************/
    //OR OPEN THE PRE-FILLED FORM
    URL prefilled_url = new URL(my_google_form_user_url + "?usp=pp_url&" + postData);
    if(Desktop.isDesktopSupported()){
        Desktop.getDesktop().browse(prefilled_url.toURI());}

}catch (IOException e1){e1.printStackTrace();}

但我想知道URL的长度是否有限制,当我用“桌面”打开它时以及当我发出POST请求时?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用我的宠物项目通过Java提交Google表单:

https://github.com/stepio/jgforms

可能的限制主要是由HTTP标准定义的,因此此问题可能会对您有所帮助:

What is the maximum length of a URL in different browsers?

根据我的经验,我成功地使用表格为Android应用程序实现了“记录器”:记录了完整的堆栈跟踪记录以及说明性消息。