本文介绍了在Google Drive + Java中使用Google Spreadsheet API创建电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已通过Google官方文档中提到的简单Java代码成功在我的Google云端硬盘帐户的现有电子表格中创建了一个新工作表,该代码位于,但我想通过Java代码在我的Google云端硬盘帐户中创建新电子表格。在提到的链接中,他们没有提到任何示例代码。我经历了很多链接,但没有得到如何做?我已经看到了Spreadservice类中的不同方法。



我不知道如何处理Google Spreadsheet API?

  / * 
*要更改此模板,请选择工具|模板
*并在编辑器中打开模板。
* /

导入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;
导入java.net。*;
import java.util。*;
import javax.xml.soap.Text;

$ b $ **
*
* @author satyam
* /
公共类SpreadSheet {

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

SpreadsheetService service = new SpreadsheetService(gBuddy);

// TODO:为特定用户授权服务对象(请参阅其他部分)
字符串USERNAME =USERNAME;
String PASSWORD =PASSWORD;

service.setUserCredentials(USERNAME,PASSWORD);


//定义要请求的网址。这应该永远不会改变。
URL SPREADSHEET_FEED_URL =新网址(
https://spreadsheets.google.com/feeds/spreadsheets/private/full);

//向API发出请求并获取所有电子表格。
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,SpreadsheetFeed.class);
列表< SpreadsheetEntry> spreadsheets = feed.getEntries();

//遍历所有电子表格返回
(SpreadsheetEntry电子表格:电子表格){
//将此电子表格的标题打印到屏幕上;
System.out.println(spreadsheet.getTitle()。getPlainText());
}

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

// //创建新工作表的本地表示。
WorksheetEntry worksheet = new WorksheetEntry();
worksheet.setTitle(new PlainTextConstruct(New Worksheet));
worksheet.setColCount(10);
worksheet.setRowCount(20);

//将工作表的本地表示形式发送到
//创建的API。此处使用的网址是我们的
//电子表格的工作表供稿网址。
URL worksheetFeedUrl = spreadsheet.getWorksheetFeedUrl();
WorksheetEntry insert = service.insert(worksheetFeedUrl,worksheet);


$ / code>


解决方案

经过一番调查,我发现这个答案很简单。我们无法使用 Google Spreadsheet API 在Google云端硬盘中创建新的电子表格。 在Google Spreadsheet API的Google Drive驱动器已存在的电子表格中,但无法使用电子表格API创建新的电子表格。



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



这就是我正在寻找的。通过这个,我们可以在谷歌驱动器中使用谷歌驱动器api创建一个新的电子表格。
$ b

  DocsService docsService = new DocsService(MySampleApplication-v3 ); 
docsService.setUserCredentials(USERNAME,PASSWORD);
网址GOOGLE_DRIVE_FEED_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() code> object和任何其他文档,我们必须创建新的DocumentEntry()对象。



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

  / /文件上传在谷歌驱动器
DocumentListEntry uploadFile = new DocumentListEntry();
uploadFile.setTitle(new PlainTextConstruct(FILE_NAME_DISPLAY_IN_DRIVE));
uploadFile.setFile(新文件(FILE_PATH),MIME_TYPE_OF_FILE);
uploadFile = docsService.insert(GOOGLE_DRIVE_FEED_URL,uploadFile);


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.

I am not getting how to do with 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);
        }
    }
解决方案

It's just simple after some research I found this answer. We cannot create a new spreadsheet in google drive with Google Spreadsheet 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.

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

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

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

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 Drive + Java中使用Google Spreadsheet API创建电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:59