edit google document remotely

与世无争的帅哥 提交于 2019-12-05 20:55:10

Yes, it is possible.

First write an Apps script that changes your desired document. Then deploy it as a web-app running as you that anyone has access, even anonymous. Check out the guides at Apps Script page to see how to write your script as a web-app.

Your script will then have a public url, which you can call from your website and have it run "remotely" normally.

To provide an example of what Henrique suggests, here is a small webapp that adds text to a publicly viewable document I own (it doesn't need to be public except for anyone here to check it works !)

I wrote it using UiApp but you could of course use HTMLService if you prefer...

The app runs as me but is accessible to anyone even anonymous.

// publicly viewable test doc url : https://docs.google.com/document/d/1THzBTURxGr2CdUmcZ7i2zD-RM8I3im2JCSHI3BHlkeM/edit

function doGet(){
  var app = UiApp.createApplication().setTitle('docEdit');
  var panel = app.createAbsolutePanel().setSize('100%','100%').setStyleAttributes({'padding':'40px','backgroundColor':'lightBlue'});
  var text = app.createTextArea().setName('text').setPixelSize(500,300);
  var grid = app.createFlexTable().setId('grid');
  grid.setText(0,0,'Add your text').setWidget(1,0,text);  
  var handler = app.createServerHandler('writeText').addCallbackElement(panel);
  grid.setWidget(2,0,app.createButton('update document',handler).setId('btn'));
  app.add(panel.add(grid));
  return app;
}

function writeText(e){
  var doc = DocumentApp.openById('1THzBTURxGr2CdUmcZ7i2zD-RM8I3im2JCSHI3BHlkeM');
  var now = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'MMM/dd/yyyy @ hh:mm:ss');
  var body = doc.getBody();
  body.appendParagraph('Append text on '+now+' : '+e.parameter.text);
  doc.saveAndClose();
  var app = UiApp.getActiveApplication();
  var grid = app.getElementById('grid');
  grid.setWidget(3,0,app.createHTML('Thanks,<br>Your text has been added to the document'));
  app.getElementById('btn').setEnabled(false).setHTML('Button disabled');
  return app;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!