在谷歌驱动器+ Java中使用谷歌电子表格API创建电子表格

时间:2022-06-20 15:24:53

I have successfully created a new worksheet in a Existing Spreadsheet of My Google Drive Account through a simple Java code mentioned in Google's official Documentation on Developer Guide of Spreadsheet API but I wants to create a new spreadsheet in my Google drive account through java code. In mentioned link they didn't mentioned any sample code for that. I gone through many links but not getting how to do? I have already seen different methods available in Spreadservice class.

我已经成功地在我的谷歌驱动器帐户的现有电子表格中创建了一个新的工作表,通过谷歌关于电子表格API开发人员指南的官方文档中提到的简单Java代码,但是我想通过Java代码在我的谷歌驱动器帐户中创建一个新的电子表格。在上面提到的链接中,他们没有提到任何示例代码。我浏览了很多链接,但没有得到如何做?我已经在Spreadservice类中见过不同的方法。

I am not getting how to do with Google Spreadsheet API ?

我不知道如何使用谷歌电子表格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 个解决方案

#1


26  

It's just simple after some research I found this answer. We cannot create a new spreadsheet in google drive with Google Spreadsheet API.

经过一些研究,我找到了这个答案。我们不能使用谷歌电子表格API在谷歌驱动器中创建一个新的电子表格。

NOTE: we can create new worksheet in already exist spreadsheet of google drive through Google Spreadsheet API but cannot create a new spreadsheet with spreadsheet api.

注意:我们可以通过谷歌电子表格API在已经存在的谷歌驱动器上创建新的工作表,但是不能使用电子表格API创建一个新的电子表格。

For creating and uploading new Spreadsheet or any other kind of document which google drive supports we have to use Google Drive api.

要创建和上传新的电子表格或谷歌驱动器支持的任何其他类型的文档,我们必须使用谷歌驱动器api。

This is what I am looking for. By this we can create a new spreadsheet in google drive using 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);

For creating a new spreadsheet we have to create new SpreadsheetEntry() object and for any other document we have to create new DocumentEntry() object.

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

NOW If we have to upload any kind of document(xls,doc,image etc) in google drive we can do like this

现在,如果我们必须在谷歌驱动器中上传任何类型的文档(xls,doc,image等),我们可以这样做

//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);

#2


1  

You should use the Google Documents List API to create new speadsheets.

您应该使用谷歌文档列表API来创建新的speadsheets。

What can this API do?

这个API能做什么?

The Google Documents List API allows developers to create, retrieve, update, and delete Google Docs (including but not limited to text documents, spreadsheets, presentations, and drawings), files, and collections. It also provides some advanced features like resource archives, Optical Character Recognition, translation, and revision history.

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

#1


26  

It's just simple after some research I found this answer. We cannot create a new spreadsheet in google drive with Google Spreadsheet API.

经过一些研究,我找到了这个答案。我们不能使用谷歌电子表格API在谷歌驱动器中创建一个新的电子表格。

NOTE: we can create new worksheet in already exist spreadsheet of google drive through Google Spreadsheet API but cannot create a new spreadsheet with spreadsheet api.

注意:我们可以通过谷歌电子表格API在已经存在的谷歌驱动器上创建新的工作表,但是不能使用电子表格API创建一个新的电子表格。

For creating and uploading new Spreadsheet or any other kind of document which google drive supports we have to use Google Drive api.

要创建和上传新的电子表格或谷歌驱动器支持的任何其他类型的文档,我们必须使用谷歌驱动器api。

This is what I am looking for. By this we can create a new spreadsheet in google drive using 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);

For creating a new spreadsheet we have to create new SpreadsheetEntry() object and for any other document we have to create new DocumentEntry() object.

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

NOW If we have to upload any kind of document(xls,doc,image etc) in google drive we can do like this

现在,如果我们必须在谷歌驱动器中上传任何类型的文档(xls,doc,image等),我们可以这样做

//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);

#2


1  

You should use the Google Documents List API to create new speadsheets.

您应该使用谷歌文档列表API来创建新的speadsheets。

What can this API do?

这个API能做什么?

The Google Documents List API allows developers to create, retrieve, update, and delete Google Docs (including but not limited to text documents, spreadsheets, presentations, and drawings), files, and collections. It also provides some advanced features like resource archives, Optical Character Recognition, translation, and revision history.

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