Google Drive API V3 (javascript) update file contents

后端 未结 3 557
灰色年华
灰色年华 2021-01-04 05:28

I want to update the contents of a Google doc using the Google Drive API V3 (javascript):

https://developers.google.com/drive/v3/reference/files/update

I\'m

3条回答
  •  情话喂你
    2021-01-04 05:51

    I build gDriveSync.js library to sync with google drive using javascript v3 api https://github.com/vitogit/gDriveSync.js

    You can check the sourcecode of what I did (https://github.com/vitogit/gDriveSync.js/blob/master/lib/drive.service.js) , basically is a 2 step process, first you create the file and then you update it.

      this.saveFile = function(file, done) {
        function addContent(fileId) {
          return gapi.client.request({
              path: '/upload/drive/v3/files/' + fileId,
              method: 'PATCH',
              params: {
                uploadType: 'media'
              },
              body: file.content
            })
        }
        var metadata = {
          mimeType: 'application/vnd.google-apps.document',
          name: file.name,
          fields: 'id'
        }
        if (file.parents) {
          metadata.parents = file.parents;
        }
    
        if (file.id) { //just update
          addContent(file.id).then(function(resp) {
            console.log('File just updated', resp.result);
            done(resp.result);
          })
        } else { //create and update
          gapi.client.drive.files.create({
            resource: metadata
          }).then(function(resp) {
            addContent(resp.result.id).then(function(resp) {
              console.log('created and added content', resp.result);
              done(resp.result);
            })
          });
        }
      }
    

提交回复
热议问题