使用JavaScript编程编辑Google文档 [英] Programmatically edit a Google doc with JavaScript

查看:137
本文介绍了使用JavaScript编程编辑Google文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是运行一些JavaScript代码,将文本输入到Google文档中。我迄今为止所做的是在嵌入Google文档的个人网页上创建一个iframe元素。我现在想要做的就是使用谷歌源代码中的函数来输入文本。



当我使用Chrome检查器和js美化器时,我发现如果我听按键事件,我会看到下面的代码段:

 函数dKa(){
var a = eKa,b = TJa?函数(c){
return a.call(b.src,b.key,c)
}:function(c){
c = a.call(b.src,b。密钥,c);
if(!c)
return c
};
return b
}

我曾尝试在加载页面时调用函数用一个简单的 onload =function();事件来试图产生一个输出,但没有发生任何事情。有没有办法做我想做的事情?我想找到正确的函数来调用,以便我可以通过该函数向文档写入文本。然而,代码似乎太混乱,无法做任何事情。



我知道要从父框架引用Google Docs(iframe)代码,我需要做如下所示: document.getElementById(iframeID)。contentWindow.theFunction()
我在执行中错了吗?据我所知,使用API​​编辑Google Doc是不可能的。



这也是我第一次在这里发布,所以如果这太模糊,让我知道,希望我能澄清。正如其他人已经注意到,相同来源的政策将阻止你从工作中采取的方法,并且试图对其进行逆向工程是愚蠢的,因为实现可能随时发生变化(或者对于不同的用户,Google推出更新时甚至会有所不同)。

然而,Google有一个称为 Google Apps脚本的确切事物的API。大多数教程都适用于电子表格,但对于DocumentApp,DocsList,Document有文档等等。你可能不得不重新思考你的方法,但你仍然应该能够实现你的目标。


What I'm trying to do is run some JavaScript code that will enter text into a Google doc. What I have done so far is create an iframe element on my personal web page that embeds Google Docs. What I want to do, for now, is use functions from Google's source code to input the text.

When I use the Chrome inspector along with a js beautifier, I found that if I listen for a keypress event, I get brought to the following code segment:

function dKa() {
   var a = eKa, b = TJa ? function(c) {
      return a.call(b.src, b.key, c)
   } : function(c) {
      c = a.call(b.src, b.key, c);
      if (!c)
      return c
   };
   return b
}

I have tried calling the function upon loading the page with a simple onload="function();" event in a futile attempt to yield an output, but nothing happened. Is there a way to do what I'm trying to do? I want find the correct function to call, so that I may write text to the doc via that funcion. However, the code seems too obfuscated to do anything with.

I know that to refer to the Google Docs (the iframe) code from the parent frame, I need to do the following: document.getElementById("iframeID").contentWindow.theFunction() Am I wrong in my execution? To my knowledge, it isn't possible to live edit a Google Doc using their API.

This is also my first time posting here, so if this is too vague, let me know and hopefully I can clarify. Thanks in advance.

解决方案

As others have noted the same-origin policy will prevent the approach you're taking from working, and trying to reverse-engineer it is folly anyway as the implementation could change at any time (or even be different for different users as Google rolls out updates).

However, Google has an API for this exact thing called Google Apps Script. Most of the tutorials are geared toward spreadsheets, but there is documentation for DocumentApp, DocsList, Document, etc. You may have to rethink your approach a bit but you should still be able to accomplish your goals.

这篇关于使用JavaScript编程编辑Google文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆