、それがお役に立てば幸いですが、書き込みを実行するために、クイックスタートチュートリアルコードの修正版です。
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;
私はこのラインでの警告の権限を取得し、それはあなたがよ、
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/sheets.googleapis.com.json");
クイックスタートチュートリアルでは、クイックスタートチュートリアルごとの読み取り専用スコープ代わり
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から証明書ファイルをダウンロードする必要があります。
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
}
}
もう一つの注目すべき - 私は私のプロジェクトにサーブレット-api.jarとを追加する必要がありました。
[最初の結果](https://developers.google.com/google-apps/spreadsheets/)セルを書き出す方法についての情報があるようです。 – CodeChimp
API/Java DOCSを調べてください。あなたは – JavaHopper
CodeChimpを理解することができます。あなたはレガシーコードにリンクしています。私は、現在の最新のバージョンの作業例に興味があります。ありがとうございました –