在Google Drive中使用Google Spreadsheets API创建电子表格

14

我已经成功地通过 Google 开发者指南 Sheets API 中提到的简单 Java 代码,在我的 Google Drive 帐户中现有电子表格中创建了一个新的工作表,但是我想通过 Java 代码在我的 Google Drive 帐户中创建一个新的电子表格。在链接中,他们没有提供任何此方面的示例代码。我已经查看了 Spreadservice 类中可用的不同方法。

如何使用 Google Spreadsheets API 实现这一点?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.Link;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.docs.ExportFormat;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;


import java.io.IOException;
import java.net.*;
import java.util.*;
import javax.xml.soap.Text;


    /**
     *
     * @author satyam
     */
    public class SpreadSheet {

        public static void main(String[] args)
                throws AuthenticationException, MalformedURLException, IOException, ServiceException {

            SpreadsheetService service =   new SpreadsheetService("gBuddy");

            // TODO: Authorize the service object for a specific user (see other sections)
            String USERNAME = "USERNAME";
            String PASSWORD = "PASSWORD";

            service.setUserCredentials(USERNAME, PASSWORD);


            // Define the URL to request.  This should never change.
            URL SPREADSHEET_FEED_URL = new URL(
                    "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

            // Make a request to the API and get all spreadsheets.
            SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
            List<SpreadsheetEntry> spreadsheets = feed.getEntries();

            // Iterate through all of the spreadsheets returned
            for (SpreadsheetEntry spreadsheet : spreadsheets) {
    //             Print the title of this spreadsheet to the screen;
                System.out.println(spreadsheet.getTitle().getPlainText());
            }

            SpreadsheetEntry spreadsheet = spreadsheets.get(1);
    //        System.out.println(spreadsheet.getTitle().getPlainText());

    //        // Create a local representation of the new worksheet.
            WorksheetEntry worksheet = new WorksheetEntry();
            worksheet.setTitle(new PlainTextConstruct("New Worksheet"));
            worksheet.setColCount(10);
            worksheet.setRowCount(20);

            // Send the local representation of the worksheet to the API for
            // creation.  The URL to use here is the worksheet feed URL of our
            // spreadsheet.
            URL worksheetFeedUrl = spreadsheet.getWorksheetFeedUrl();
            WorksheetEntry insert = service.insert(worksheetFeedUrl, worksheet);
        }
    }
2个回答

28

经过一些研究,我发现这个答案很简单。我们无法使用Google Spreadsheet API在Google Drive中创建新电子表格。

注意:我们可以通过Google Spreadsheet API在已有的Google Drive电子表格中创建新工作表,但无法使用电子表格API创建新的电子表格。

要创建和上传新电子表格或任何其他类型的Google Drive支持的文档,我们必须使用Google Drive API

这就是我要找的。通过这种方式,我们可以使用Google Drive API在Google Drive中创建新的电子表格。

    DocsService docsService = new DocsService("MySampleApplication-v3");
    docsService.setUserCredentials(USERNAME, PASSWORD);
    URL GOOGLE_DRIVE_FEED_URL = new URL("https://docs.google.com/feeds/default/private/full/");
    DocumentListEntry documentListEntry = new com.google.gdata.data.docs.SpreadsheetEntry();
    documentListEntry.setTitle(new PlainTextConstruct("Spreadsheet_name"));
    documentListEntry = docsService.insert(GOOGLE_DRIVE_FEED_URL, documentListEntry);

为了创建一个新的电子表格,我们必须创建new SpreadsheetEntry()对象,对于任何其他文档,我们必须创建new DocumentEntry()对象。

现在,如果我们要上传任何类型的文档(如xls、doc、图像等)到谷歌云端硬盘,我们可以这样做:

//File upload in google drive
        DocumentListEntry uploadFile = new DocumentListEntry();
        uploadFile.setTitle(new PlainTextConstruct("FILE_NAME_DISPLAY_IN_DRIVE"));
        uploadFile.setFile(new File("FILE_PATH"), "MIME_TYPE_OF_FILE");
        uploadFile = docsService.insert(GOOGLE_DRIVE_FEED_URL, uploadFile);

但是你如何处理这个错误?com.google.gdata.util.InvalidEntryException: Entry must contain exactly one primary category - Chloe
不得不使用完全限定的包名。有两个SpreadsheetEntry类。谷歌搞什么鬼? entry.getCategories().add(com.google.gdata.data.docs.SpreadsheetEntry.CATEGORY); - Chloe

1

您应该使用Google Documents List API来创建新的电子表格。

这个API能做什么?

Google文档列表API允许开发人员创建、检索、更新和删除Google文档(包括但不限于文本文档、电子表格、演示文稿和绘图),文件和集合。它还提供了一些高级功能,如资源存档、光学字符识别、翻译和修订历史记录。


1
我已经检查了这个链接,但是如何在Java中实现呢?因为示例代码是在.NET而不是Java中。我甚至尝试在Java API中寻找相同的类,但没有成功。 - Satyam Koyani
1
很遗憾,此 API 已被弃用。你应该使用 Google Drive API - user1067920

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接