在 android studio 中以编程方式创建电子表格

create spreadsheet programmatically in android studio

我想在 android studio 中以编程方式创建电子表格。我怎样才能做到这一点? 我使用 OAuth 登录用户,现在想在他的驱动器文件夹中创建电子表格。

我找到了下面的代码,但不知道如何使用它...

Spreadsheet spreadsheet = new Spreadsheet()
    .setProperties(new SpreadsheetProperties()
    .setTitle(title));
spreadsheet = service.spreadsheets().create(spreadsheet)
    .setFields("spreadsheetId")
    .execute();
System.out.println("Spreadsheet ID: " + spreadsheet.getSpreadsheetId());

要以编程方式创建 Google Sheet,您应该尝试使用 Sheet API. More specifcally you need to use the create 方法。

如果您正在为 Java 使用 Google 库,您可以尝试按照 create 端点中的示例进行操作:

/*
 * BEFORE RUNNING:
 * ---------------
 * 1. If not already done, enable the Google Sheets API
 *    and check the quota for your project at
 *    https://console.developers.google.com/apis/api/sheets
 * 2. Install the Java client library on Maven or Gradle. Check installation
 *    instructions at https://github.com/google/google-api-java-client.
 *    On other build systems, you can add the jar files to your project from
 *    https://developers.google.com/resources/api-libraries/download/sheets/v4/java
 */
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.Spreadsheet;
import java.io.IOException;
import java.security.GeneralSecurityException;

public class SheetsExample {
  public static void main(String args[]) throws IOException, GeneralSecurityException {
    // TODO: Assign values to desired fields of `requestBody`:
    Spreadsheet requestBody = new Spreadsheet();

    Sheets sheetsService = createSheetsService();
    Sheets.Spreadsheets.Create request = sheetsService.spreadsheets().create(requestBody);

    Spreadsheet response = request.execute();

    // TODO: Change code below to process the `response` object:
    System.out.println(response);
  }

  public static Sheets createSheetsService() throws IOException, GeneralSecurityException {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    // TODO: Change placeholder below to generate authentication credentials. See
    // https://developers.google.com/sheets/quickstart/java#step_3_set_up_the_sample
    //
    // Authorize using one of the following scopes:
    //   "https://www.googleapis.com/auth/drive"
    //   "https://www.googleapis.com/auth/drive.file"
    //   "https://www.googleapis.com/auth/spreadsheets"
    GoogleCredential credential = null;

    return new Sheets.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("Google-SheetsSample/0.1")
        .build();
  }
}

此示例未处理凭据,您需要更改它才能正常工作

如果您需要图书馆中的更多信息,请阅读 Java Quickstart to work from there, also look at the java create method。当然,如果您已经管理了所有 OAuth 部分,您可以只执行 HTTP 请求。