简体   繁体   English

使用 Google 文档 API

[英]Using the Google Docs API

I want to know how to upload a remote document to Google Docs, and to be able to view that document using Google Docs by using the Google Docs API.我想知道如何将远程文档上传到 Google Docs,并能够通过 Google Docs API 使用 Google Docs 查看该文档。

When I upload a file, does is respond with a docid?当我上传文件时,是否会以 docid 响应? So that I can use it to view the document online after uploading in to my account.这样我可以在上传到我的帐户后使用它在线查看文档。

For instance I can open one of the documents in my account by using the docid例如,我可以使用 docid 打开我帐户中的文档之一

https://docs.google.com/Doc?docid=0AdmWTLTOoWVBZGd3ZDdtZmhfMGZ3czNrNmho https://docs.google.com/Doc?docid=0AdmWTLTOoWVBZGd3ZDdtZmhfMGZ3czNrNmho

Will this be implementable in PHP?这可以在 PHP 中实现吗? I heard its no longer supported.我听说它不再支持。 I will prefer PHP over C#, but if PHP is not supported I can use C#.我会更喜欢 PHP 而不是 C#,但如果不支持 PHP,我可以使用 C#。

Step 1: create a new google document in the programming language of your choice using the documentCreate method:第 1 步:使用documentCreate方法以您选择的编程语言创建一个新的 google 文档:
reference: document create参考: 文档创建
(This reference describes the raw http request, there are APIS for numerous languages.) (此参考描述了原始 http 请求,有多种语言的 APIS。)

Step 2: read the text of your input file and insert the text into the Google document with the insertText method:第 2 步:读取输入文件的文本并使用insertText方法将文本插入到 Google 文档中:
golang 戈朗
javascript javascript

Step 3: add styling elements to the google document, if you care to make the document look pretty.第 3 步:添加样式元素到 google 文档,如果你想让文档看起来更漂亮的话。

You have to download the file local using the following code.您必须使用以下代码在本地下载文件。

var request = new DocumentsRequest(settings);
request.BaseUri = "https://docs.google.com/feeds/default/private/full/folder" + folderResourceId + "/contents";
Feed<Document> feed = request.GetEverything();

foreach (Document entry in feed.Entries)
{
    if (entry.Title == fileName)
    {
        var fI = new FileInfo(uploadpath + entry.Title);
        Stream stream = request.Download(entry, "");
        arr = Read(stream);
        stream.Close();
        break;
     }
}

private Byte[] Read(Stream stream)
{
    //long originalPosition = stream.Position;
    //stream.Position = 0;

    try
    {
        Byte[] readBuffer = new Byte[4096];

        int totalBytesRead = 0;
        int bytesRead;

        while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
        {
            totalBytesRead += bytesRead;

            if (totalBytesRead == readBuffer.Length)
            {
                int nextByte = stream.ReadByte();
                if (nextByte != -1)
                {
                    Byte[] temp = new Byte[readBuffer.Length * 2];
                    Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                    Buffer.SetByte(temp, totalBytesRead, (Byte)nextByte);
                    readBuffer = temp;
                    totalBytesRead++;
                }
            }
        }

        Byte[] buffer = readBuffer;
        if (readBuffer.Length != totalBytesRead)
        {
            buffer = new Byte[totalBytesRead];
            Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
        }
        return buffer;
    }
    finally
    {
        //stream.Position = originalPosition;
    }
}

After download the file save it to local system and display using asp.net下载文件后保存到本地系统并使用asp.net显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM