使用Google云端硬盘中的Google Spreadsheets API创建电子表格 [英] Create spreadsheet using Google Spreadsheets API in Google Drive

查看:106
本文介绍了使用Google云端硬盘中的Google Spreadsheets API创建电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过Google在Developer Guide Sheets API的官方文档中提到的简单Java代码在My Google Drive帐户的现有电子表格中成功创建了一个新的工作表,但是我想通过Java代码在Google Drive帐户中创建一个新的电子表格.在链接中,他们没有提及任何示例代码.我已经在Spreadservice类中看到了不同的方法.

I have successfully created a new worksheet in an existing spreadsheet of My Google Drive account through a simple Java code mentioned in Google's official documentation on Developer Guide Sheets API but want to create a new spreadsheet in my Google Drive account through Java code. In the link they didn't mention any sample code for that. I have already seen different methods available in Spreadservice class.

如何使用Google Spreadsheets API做到这一点?

How to do this with 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云端硬盘中创建新的电子表格.

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

注意::我们可以通过Google Spreadsheet API在已经存在的Google驱动器电子表格中创建新工作表,但无法使用电子表格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.

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

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

这就是我要寻找的.这样,我们可以使用Google Drive API在Google Drive中创建一个新的电子表格.

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

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

现在,如果我们必须在Google驱动器中上载任何类型的文档(xls,doc,图像等),我们可以这样做

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

//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云端硬盘中的Google Spreadsheets API创建电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆