猿问

如何使用 Sheets APIv4 Java 在 Google 表格中插入一行

如果我想在指定索引处使用 Java 将一行插入到 Google 工作表中,然后写入该行,我将如何处理?



萧十郎
浏览 121回答 2
2回答

UYOU

经过大量的谷歌搜索,一些挣扎和适应另一个答案后,我能够弄清楚这一点。注意:您可以生成自己的credentials.json. 这位于大多数 IDE 中的“资源”文件夹中。这是我使用的完整代码——您需要的相关方法是insertRow下面的方法,该方法是从调用的。writeTest 在正常情况下,我只会发布有问题的方法,但是工作表 API 中的“快速启动”肯定是缺乏的所以这里是完整的来源:类和进口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.javanet.NetHttpTransport;import com.google.api.client.json.JsonFactory;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.*;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.security.GeneralSecurityException;import java.util.Arrays;import java.util.Collections;import java.util.List;public class SheetsTest {&nbsp; &nbsp; private static final String APPLICATION_NAME = "Metrics project";&nbsp; &nbsp; private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();&nbsp; &nbsp; private static final String TOKENS_DIRECTORY_PATH = "tokens";&nbsp; &nbsp; private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS);&nbsp; &nbsp; private static final String CREDENTIALS_FILE_PATH = "/credentials.json";获取凭据:private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException{&nbsp; &nbsp; InputStream in = SheetsTest.class.getResourceAsStream(CREDENTIALS_FILE_PATH);&nbsp; &nbsp; GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));&nbsp; &nbsp; GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setAccessType("offline")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();&nbsp; &nbsp; return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");}写测试//Creates a new row at row 2 then writes data to it...static void writeTest(Report r, String sheetID, String range)throws GeneralSecurityException, IOException{&nbsp; &nbsp; final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();&nbsp; &nbsp; ValueRange requestBody = requestBuilder(r, range);&nbsp; &nbsp; Sheets sheetsService = new Sheets.Builder(HTTP_TRANSPORT,JSON_FACTORY, getCredentials(HTTP_TRANSPORT))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setApplicationName(APPLICATION_NAME)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; insertRow(sheetsService, sheetID);&nbsp; &nbsp; Sheets.Spreadsheets.Values.Append request =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sheetsService.spreadsheets().values().append(sheetID, range, requestBody);&nbsp; &nbsp; request.setValueInputOption("RAW");&nbsp; &nbsp; //request.setInsertDataOption("INSERT_ROWS");&nbsp; &nbsp; AppendValuesResponse resp = request.execute();&nbsp; &nbsp; System.out.println(resp);}插入行//Inserts row at index, this is hardcoded to row 2 (0-indexed)private static void insertRow(Sheets ss, String sheetID)throws IOException{&nbsp; &nbsp; InsertDimensionRequest insertRow = new InsertDimensionRequest();&nbsp; &nbsp; insertRow.setRange(new DimensionRange().setDimension("ROWS").setStartIndex(1).setEndIndex(2).setSheetId(0));&nbsp; &nbsp; BatchUpdateSpreadsheetRequest r = new BatchUpdateSpreadsheetRequest().setRequests(Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Request().setInsertDimension(insertRow)&nbsp; &nbsp; ));&nbsp; &nbsp; ss.spreadsheets().batchUpdate(sheetID, r).execute();}请求生成器//Populate ValueRangestatic ValueRange requestBuilder( Report r, String range){&nbsp; &nbsp; ValueRange v = new ValueRange()&nbsp; &nbsp; &nbsp; &nbsp; .setValues(Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(r.value1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(r.value2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(r.value3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(r.value4),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(r.value5)&nbsp; &nbsp; &nbsp; &nbsp; ))&nbsp; &nbsp; &nbsp; &nbsp; .setMajorDimension("ROWS")&nbsp; &nbsp; &nbsp; &nbsp; .setRange(range)&nbsp; &nbsp; ;&nbsp; &nbsp; return v;}

潇湘沐

我不允许对 Elric 的回答发表评论,但我只想更正一下。要使用您的值添加单行,请在方法中:请求生成器\它应该.setMajorDimension("COLUMNS")代替"ROWS.我希望这是有帮助的。
随时随地看视频慕课网APP

相关分类

Java
我要回答