Google Sheet API:仅在第一张纸上打印-Java

时间:2018-06-22 20:31:42

标签: java servlets google-sheets

我能够操纵第一张纸上的数据,但不能操纵第二张纸上的数据。在API页面上,它指出范围必须指示工作表标签名称。所以我做到了,它可以编译并运行,但是它将数据输入到Sheet1而不是Sheet 2。

标签如下:

This is what the tabs look like

对的任何帮助深表谢意,在此先谢谢您

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.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.*;
import com.google.api.services.sheets.v4.Sheets;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class SheetsQuickstart {
private static final String APPLICATION_NAME = "CSC131";
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".credentials//sheets.googleapis.com-java-quickstart.json");
private static FileDataStoreFactory DATA_STORE_FACTORY;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static HttpTransport HTTP_TRANSPORT;
private static final List<String> SCOPES = Arrays.asList( SheetsScopes.SPREADSHEETS );
private static String spreadsheetId = "1BLt5iz1udvdrbxPAoHbgxNtpqgKmV_fvvAKGWPJekyM";

static {
    try {
        HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}

public static Credential authorize() throws IOException {
    InputStream in = new FileInputStream("client_secret.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}


public static Sheets getSheetsService() throws IOException {
    Credential credential = authorize();
    return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
} 


/********************* Stores comment in cell *****************/
public static void inputComment(int col, String Comment, String ID)throws IOException{
    Sheets service = getSheetsService();
    final String range = "Sheet Two!C2:C26";
    ValueRange response = service.spreadsheets().values().get(spreadsheetId, range).execute();
    List<List<Object>> element = response.getValues();
    int cellRow = 0;
    for (List row : element) {
        cellRow++;
        if(ID.equals(row.get(0))){
            printInfo(cellRow,col,Comment);
            break;
        }           
    } 
}



/********************* Prints everything *****************/
public static void printInfo(int row, int count, String Date) throws IOException{
    Sheets service = getSheetsService();
    List<Request> requests = new ArrayList<>();

    List<CellData> values = new ArrayList<>();
    values.add(new CellData().setUserEnteredValue(new ExtendedValue().setStringValue((Date))));
    requests.add(new Request()
        .setUpdateCells(new UpdateCellsRequest()
        .setStart(new GridCoordinate().setSheetId(0).setRowIndex(row).setColumnIndex(count+6))
        .setRows(Arrays.asList(new RowData().setValues(values)))
        .setFields("userEnteredValue,userEnteredFormat.backgroundColor")));     
    BatchUpdateSpreadsheetRequest batchUpdateRequest = new BatchUpdateSpreadsheetRequest().setRequests(requests);
    service.spreadsheets().batchUpdate(spreadsheetId, batchUpdateRequest).execute();
}

}

1 个答案:

答案 0 :(得分:0)

问题似乎出在$ grep Cluster log.* log.17:09:38:Cluster one, line one log.17:09:38:Cluster one, line two log.17:09:40:Cluster two, line one log.17:09:40:Cluster two, line two 中-请注意printInfo()

如果有疑问,sheetId来自new GridCoordinate().setSheetId(0)-例如来自SheetProperties