簡體   English   中英

使用Google雲端硬盤中的Google Spreadsheets API創建電子表格

[英]Create spreadsheet using Google Spreadsheets API in Google Drive

我已經通過Google開發人員指南表API上的官方文檔中提到的簡單Java代碼在My Google雲端硬盤帳戶的現有電子表格中成功創建了一個新工作表,但我想通過Java代碼在我的Google雲端硬盤帳戶中創建一個新的電子表格。 在鏈接中,他們沒有提到任何示例代碼。 我已經在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);
        }
    }

經過一些研究后我發現這個答案很簡單。 我們無法使用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);

為了創建新的電子表格,我們必須創建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);

您應該使用Google文檔列表API來創建新的speadsheets。

這個API可以做什么?

Google文檔列表API允許開發人員創建,檢索,更新和刪除Google文檔(包括但不限於文本文檔,電子表格,演示文稿和繪圖),文件和集合。 它還提供了一些高級功能,如資源存檔,光學字符識別,翻譯和修訂歷史記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM