Google docs api以编辑模式打开文档

时间:2014-01-31 10:16:11

标签: javascript google-drive-api google-docs-api

我正在使用Quickstart: Run a Drive app in JavaScript教程将文档上传到Google云端硬盘。我成功地能够运行HTML文件并上传文档。

例如,我上传了一个test.docx文件,而JSON响应位于帖子的末尾

问题是,备用链接仅在视图中打开文档(pdf like)模式。 对于这个例子,这里是链接 https://docs.google.com/a/easytha.com/file/d/0B7x-2AmDcIyRN3hPVVRWSUhQaWs/edit

但是我如何构建一个URL,以便它在这样的编辑模式下打开 https://docs.google.com/a/easytha.com/document/d/1rH79lwfX4bZ4R5EEhSaTy52dfdKiezNOaaR-6ORBcoI/edit

这两个都是相同的文件。

alternateLink
    "https://docs.google.com...hQaWs/edit?usp=drivesdk"

appDataContents
    false

copyable
    true

createdDate
    "2014-01-31T10:01:31.092Z"

downloadUrl
    "https://doc-00-10-docs....4259&e=download&gd=true"

editable
    true

etag
    ""GJcqFgACxaboeo6aKKeP6-mh0PE/MTM5MTE2MjQ5MDg1NQ""

fileExtension
    "docx"

fileSize
    "12497"

iconLink
    "https://ssl.gstatic.com...s/icon_10_word_list.png"

id
    "0B7x-2AmDcIyRN3hPVVRWSUhQaWs"

kind
    "drive#file"

labels
    Object { starred=false, hidden=false, trashed=false, more...}

lastModifyingUser
    Object { kind="drive#user", displayName="sapan parikh", isAuthenticatedUser=true, more...}

lastModifyingUserName
    "sapan parikh"

lastViewedByMeDate
    "2014-01-31T10:01:30.855Z"

md5Checksum
    "0acf21000754601319600c5182d794d4"

mimeType
    "application/vnd.openxml...rdprocessingml.document"

modifiedByMeDate
    "2014-01-31T10:01:30.855Z"

modifiedDate
    "2014-01-31T10:01:30.855Z"

originalFilename
    "test.docx"

ownerNames
    ["sapan parikh"]

owners
    [Object { kind="drive#user", displayName="sapan parikh", isAuthenticatedUser=true, more...}]

parents
    [Object { kind="drive#parentReference", id="0ALx-2AmDcIyRUk9PVA", selfLink="https://content.googlea...nts/0ALx-2AmDcIyRUk9PVA", more...}]

quotaBytesUsed
    "12497"

selfLink
    "https://content.googlea...2AmDcIyRN3hPVVRWSUhQaWs"

shared
    false

title
    "test.docx"

userPermission
    Object { kind="drive#permission", etag=""GJcqFgACxaboeo6aKKeP6-...XMO0VKCtlQA-JDZdgem3VQ"", id="me", more...}

webContentLink
    "https://docs.google.com...SUhQaWs&export=download"

writersCanShare
    true

2 个答案:

答案 0 :(得分:2)

我也跑到了这里。问题是Google云端硬盘只能编辑Google云端硬盘文档。因此,当您上传docx文件时,它会创建一个新的Google云端硬盘文档,并在您想要编辑时重定向到此文件。这意味着它是一个不同的文件。我不确定JavaScript API,但在C#中有一个选项可以在上传时转换文档。

答案 1 :(得分:0)

也许这有点晚了,但可能有人帮助......

这是使用googleapis模块...

在nodejs中执行此操作的方法
//imports
var google = require('googleapis');
var key = require(path.join(__dirname, 'myGoogleAppJWTtoken.json'));

//method
var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/drive'], null);
var drive = google.drive('v3');

drive.files.create({
            auth: jwtClient, // jwt auth variable
            resource: { 
                name: 'DemoDoc', // saved name & google docs mimetype below
                mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' 
            },
            media: {
                mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                body: fs.createReadStream('DemoDoc.docx') // my document
            }
        }, function(err,res){
            if(err){
                throw err;
            }else{
                console.log("SuccessFull...");
            }
        });