使用Google云端硬盘+ Java中的Google Spreadsheet API创建电子表格

Sat*_*ani 13 java google-sheets google-docs-api google-sheets-api

我已经通过Google官方文档开发人员指南Spreadsheet API中提到的简单Java代码在My Google Drive帐户的现有电子表格中成功创建了一个新的工作表,但我想创建一个新的电子表格通过java代码在我的Google云端硬盘帐户中.在上面提到的链接中,他们没有提到任何示例代码.我经历过许多链接,但没有得到怎么办?我已经在Spreadservice类中看到了不同的方法.

我没有了解如何使用Google Spreadsheet 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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Sat*_*ani 28

经过一些研究后我发现这个答案很简单.我们无法使用Google Spreadsheet API在Google 云端硬盘中创建新的电子表格.

注意:我们可以通过Google Spreadsheet API在已存在的Google云端硬盘电子表格中创建新的工作表,但无法使用电子表格API创建新的电子表格.

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

这就是我要找的.通过这个我们可以使用谷歌驱动器API在谷歌驱动器中创建一个新的电子表格.

    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);
Run Code Online (Sandbox Code Playgroud)

为了创建新的电子表格,我们必须创建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);
Run Code Online (Sandbox Code Playgroud)