【问题标题】:create spreadsheet programmatically in android studio在 android studio 中以编程方式创建电子表格
【发布时间】:2020-04-19 22:55:00
【问题描述】:

我想在 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());

【问题讨论】:

    标签: android google-apps-script google-sheets oauth android-studio-3.0


    【解决方案1】:

    要以编程方式创建 Google 表格,您应该尝试使用Sheet API。更具体地说,您需要使用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 的库中的更多信息以便从那里开始工作,还请查看 java create method。当然,如果您已经管理了所有 OAuth 部分,则可以只执行 HTTP 请求。

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多