久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Java 將數據寫入 Google 電子表格

Write data into Google Spreadsheet w/ Java(使用 Java 將數據寫入 Google 電子表格)
本文介紹了使用 Java 將數據寫入 Google 電子表格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我迷路了.我正在嘗試將 Java 與 Google 電子表格連接起來,雖然 API 的文檔在檢索數據方面是完整的(并且工作正常),但我無法弄清楚如何寫入電子表格.

I'm lost on this one. I'm trying to connect Java with Google spreadsheet, and although the API's documentation is complete on retrieving data (and it is working fine), I am unable to figure out how to write into the spreadsheet.

誰能提供一個完整示例(包括必要的導入和所有),說明如何在 Google 電子表格中進行非常簡單的數據輸入(例如,在 A1 單元格中輸入asdf"Sheet1)?

Could anyone, please provide a full example (with the necessary imports and all) on how to do a very simple data entry into a Google Spreadsheet (say, enter "asdf" into the A1 cell of Sheet1)?

如果這樣的教程存在于某處,我找不到它 - 任何指針將不勝感激.

If a tutorial like this exists somewhere, I could not find it - any pointers would be much appreciated.

非常感謝,Zsolt

推薦答案

這里是修改版的快速入門教程代碼,用來寫.

Here is a modified version of the quick start tutorial code, to perform a write.

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;
import java.io.*;
import java.util.*;

public class SheetsIntegration {

    private static HttpTransport transport;
    private static JacksonFactory jsonFactory;
    private static FileDataStoreFactory dataStoreFactory;

我在這一行收到了權限警告,但這不是致命的

I get a permissions warning at this line, but it's not fatal

    private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/sheets.googleapis.com.json");

快速入門教程改用只讀作用域

Quick start tutorial uses readonly scope instead

    private static List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);

    public SheetsIntegration() {
        try {
            transport = GoogleNetHttpTransport.newTrustedTransport();
            dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
            jsonFactory = JacksonFactory.getDefaultInstance();

            service = getSheetsService();
        } catch (Exception e) {
            // handle exception
        }
    }

根據快速入門教程,您需要從 Google 下載認證文件.

Per the quick start tutorial, you'll need to download the certification file from Google.

public static Credential authorize() throws IOException {
    // Load client secrets.
    File cfile = new File("certs/cert.json");
    cfile.createNewFile();
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(new FileInputStream(cfile)));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    transport, jsonFactory, clientSecrets, scopes)
                    .setDataStoreFactory(dataStoreFactory)
                    .setAccessType("offline")
                    .build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    return credential;
}

public static Sheets getSheetsService() throws IOException {
    Credential credential = authorize();
    return new Sheets.Builder(transport, jsonFactory, credential)
            .setApplicationName("INSERT_YOUR_APPLICATION_NAME")
            .build();
}

public void writeSomething(List<Data> myData) {

    try {
        String id = "INSERT_SHEET_ID";
        String writeRange = "INSERT_SHEET_NAME!A3:E";

        List<List<Object>> writeData = new ArrayList<>();
        for (Data someData: myData) {
            List<Object> dataRow = new ArrayList<>();
            dataRow.add(someData.data1);
            dataRow.add(someData.data2);
            dataRow.add(someData.data3);
            dataRow.add(someData.data4);
            dataRow.add(someData.data5);
            writeData.add(dataRow);
        }

        ValueRange vr = new ValueRange().setValues(writeData).setMajorDimension("ROWS");
        service.spreadsheets().values()
                .update(id, writeRange, vr)
                .setValueInputOption("RAW")
                .execute();
    } catch (Exception e) {
        // handle exception
    }
}

另一個注意事項 - 我必須將 servlet-api.jar 添加到我的項目中.

One other note - I had to add servlet-api.jar to my project.

這篇關于使用 Java 將數據寫入 Google 電子表格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(Google 驅動器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅動api服務賬戶查看上傳文件到谷歌驅動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 成人一区二区三区 | 亚洲精品黄 | 成人中文网 | 欧美一区免费 | 激情综合五月 | 91在线精品秘密一区二区 | 久久伊人影院 | 欧美在线一区二区三区四区 | 国产ts人妖一区二区三区 | 97伦理 | 亚洲a一区| 久久婷婷色 | 亚洲av毛片| 青青草原综合久久大伊人精品 | 中文一区 | 成人性视频在线 | 亚洲精品福利视频 | 精产国产伦理一二三区 | 国产成人99久久亚洲综合精品 | 黄a在线播放 | 亚洲精品乱码久久久久久按摩 | 九久久 | 亚洲欧美国产视频 | 国产一区二区a | 久久久国产精品一区 | 亚洲 欧美 综合 | 最新日韩在线视频 | 偷牌自拍| 一区日韩 | 正在播放国产精品 | 日韩精品在线观看视频 | 亚洲成人一区二区三区 | 五月激情综合网 | 99久久久无码国产精品 | 91在线看| 欧美性区| 久久激情视频 | 妞干网福利视频 | 欧美精品一区二区在线观看 | aaa在线观看 | 青娱乐国产 |