远程编辑谷歌文档

时间:2014-10-31 17:08:44

标签: google-apps-script google-docs google-docs-api google-drive-api

我试图在我的网站上添加一项功能,如下所示:

单击按钮会将文本附加到Google文档。

显然我需要在驱动器中创建一个Apps脚本。 问题是如何从我的网站触发Apps脚本。您可以假设我是驱动器/文档的所有者,因此有权以任何我喜欢的方式编辑它。

我看过这些主题:

  1. Workarounds : How to edit native google documents programatcally?item
  2. How to programmatically manipulate native google doc files
  3. 似乎它们都是从驱动器本身执行的操作,而不是远程触发。

    我还在Google中查找了Apps Script API,但还没有找到办法。它甚至可能吗?

2 个答案:

答案 0 :(得分:4)

是的,有可能。

首先编写一个可更改所需文档的Apps脚本。然后将其部署为任何人都可以访问的网络应用running as you,甚至是匿名的。在“Apps脚本”页面上查看the guides,了解如何将脚本编写为网络应用。

您的脚本将有一个公共网址,您可以从您的网站拨打电话,让它“正常”远程运行。

答案 1 :(得分:4)

为了提供一个Henrique建议的示例,这里有一个small webapp,它将文本添加到我自己的publicly viewable document(除了这里的任何人检查它有效之外,它不需要公开! )

我是用UiApp写的,但如果你愿意,你当然可以使用HTMLService。

enter image description here

该应用程序以我的身份运行,但即使是匿名的人也可以访问。

// 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;
}
相关问题