3

我想向我的网站添加一项功能,如下所示:远程编辑Google文档

单击按钮可以将文本追加到Google文档中。

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

我已经看过这些主题:

  1. Workarounds : How to edit native google documents programatcally?item
  2. How to programmatically manipulate native google doc files

看来他们是从驱动器本身执行的所有操作,而不是远程触发。

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

回答

4

是的,这是可能的。

首先编写一个应用程序脚本来更改所需的文档。然后将其部署为任何人均可访问的Web应用程序running as you,即使是匿名的。在Apps脚本页查看the guides,了解如何将脚本编写为Web应用程序。

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

+0

它的工作原理,但我得到'xmlhttprequest无法加载'access-control-allow-origin'错误。你知道脚本应该返回给客户吗? – sangil 2014-11-03 15:00:38

+0

你不应该有这个问题。你能分享一个问题的例子(访问一个Apps脚本公共网址)吗? – 2014-11-03 16:16:35

+0

没关系,我的坏。我从本地运行客户端而不是实际的IP地址 – sangil 2014-11-03 16:25:30

4

提供什么建议恩里克,这里的例子是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; 
} 
+0

我认为[ContentService](https://developers.google.com/apps-script/guides/content)更适合他的情况,因为他会从常规他的网站上的JavaScript。 – 2014-11-01 22:56:13

+0

我同意他是否需要从文档中返回数据,但据我了解请求 - 他只需要写入文档,以便documentApp服务是唯一参与的文档。 Web应用程序及其用户界面只是为了说明一个工作示例。 – 2014-11-02 09:15:28