以编程方式临时更改Google文档,然后替换为预先更改的版本 [英] Programmatically temporarily altering a Google Docs and then replacing with the pre-altered version

查看:140
本文介绍了以编程方式临时更改Google文档,然后替换为预先更改的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作,这似乎是一项简单的任务.经过2周的尝试各种方法后,我仍然在步骤4上失败了:

I am trying to do the following, which would seem to be a straightforward task. After 2 weeks of trying various approaches, I am still failing at Step 4:

  1. 从现有的Google文档(Gdoc)文件开始,
  2. 以编程方式接受所有建议的更改,
  3. 然后将文件导出为文本文件(此步骤很快完成)
  4. 然后撤消临时更改,以使该文件与步骤1中的开始方式相同.

我的方法(请参阅随附的代码)是将JavaScript(已将其制作为小书签)编写为:

My approach (see attached code) was to write a JavaScript (which I have made into a bookmarklet) to:

  1. 保存原始Gdoc的内容(步骤1中的版本),
  2. 然后接受建议的更改(此处是一个真正的救星,btw: http://www.tcg.com/blog/accept-all-bookmark-for-google-docs-suggestions/)
  3. 将文件本地导出为纯文本格式(全局接受建议的更改,可以将这些编辑内容合并到文本中!)
  4. 将文档还原到保存的版本.
  1. save content of the original Gdoc (the version in Step 1),
  2. then accept suggested changes (a real life-saver here, btw: http://www.tcg.com/blog/accept-all-bookmark-for-google-docs-suggestions/)
  3. export the file locally as plain text (globally accepting the suggested changes allows those edits to be incorporated into the text!)
  4. restore the document to the saved version.

Javascript代码如下.我的问题是:为什么我不通过sessionStorage.setItem保存原始文件内容,然后在最后一步中检索它以恢复文档的方法不起作用? (在这种情况下,不起作用"表示在运行脚本后查看文档时,建议的更改"不会像原始文档中那样被返回为建议").

The Javascript code follows. My question is: why doesn't my approach of saving original file content through sessionStorage.setItem, and then retrieving it to restore the document in the last step, not working? ("Not working" in this context means that when viewing the document after running the script, the "suggested changes" do not go back to being indicated as "suggestions" as in the original document).

除了sessionStorage.setItem方法外,我还尝试了许多其他方法,包括将document.getElementsByClassName("*stuff*").innerHTML.outerHTML, .innerTEXT, .outerTEXT分别存储在变量 inH,outH,inT,outT和中.脚本的开头,然后分别将document.getElementsByClassName("*stuff*").innerHTML, outerHTML, innerTEXT, .outerTEXT等同于 inH,outH .没用("材料"是"docos-icon-accept-suggestion-checkmark",顺便说一句)

In addition to the sessionStorage.setItem approach, I've tried lots of others, including storing document.getElementsByClassName("*stuff*").innerHTML, .outerHTML, .innerTEXT, .outerTEXT in variables inH, outH, inT, outT, respectively, at the beginning of the script and then equating document.getElementsByClassName("*stuff*").innerHTML, outerHTML, innerTEXT, .outerTEXT to inH, outH, resp. Didn't work. ("stuff" is "docos-icon-accept-suggestion-checkmark", btw)

我也尝试通过克隆document.getElementsByClassName("*stuff*")来制作document.getElementsByClassName("*stuff*")的备份副本":backupD = document.getElementsByClassName("*stuff*").cloneNode(true),然后再通过document.getElementsByClassName("*stuff*").replaceWith(backupD)还原也不起作用.

I've also tried making a "backup copy" of document.getElementsByClassName("*stuff*") by cloning it: backupD = document.getElementsByClassName("*stuff*").cloneNode(true) then later restoring by document.getElementsByClassName("*stuff*").replaceWith(backupD) Didn't work, either.

是否应该存储除.getElementsByClassName("*stuff*")之外的东西,然后用于还原Gdoc?我使用上面的节点,因为它是用来全局接受建议的更改的对象,这似乎是撤消"该过程的相关内容.

Is it possible that something other than .getElementsByClassName("*stuff*") is what I should be storing then using to restore the Gdoc? I'm using the above node because it is what is used to globally accept the suggested changes, which would seem to be the relevant thing to "undo" the process.

谈到撤消",我还研究了尝试以编程方式撤消"以恢复所有可接受的建议更改.我的代码知道需要执行多少次撤消"操作(当我多次手动单击Gdocs菜单上的撤消箭头时,会取回原始文档),但是我找不到任何有关如何撤消的内容在Gdoc中实现了撤消"按钮,以及如何以编程方式击中"撤消按钮.

Speaking of "undo", I also looked into trying to programmatically "undo" to restore all of the accepted suggested changes. My code knows how many times that "undo" would need to be evoked (and when I click the undo arrow on the Gdocs menu manually by that many times, I get back the original document), but I could not find anything written about how the "undo" button is implemented in Gdoc and how to programmatically "hit" the undo button.

最后,我想到的另一种选择(是我的第一个想法)是以某种方式以编程方式在Google云端硬盘上复制GDoc(那里可能有很多指南可以做到这一点),然后使用复制为我的草稿副本",用于接受所有建议的更改,因此我不必担心将原始内容还原到文件中.但是,有一个障碍是,当我启动此javascript时,它会在我单击书签时打开的文件上运行,而没有任何明显的打开方式,然后接受所有建议的更改"在复制的文件上.

Finally, the other alternative that I thought of (was my first idea to explore) was to somehow programmatically make a copy of the GDoc on the Google Drive (there's probably lots of guidance out there to do this), then use that copy as my "scratch copy" for accepting all suggested changes so that I didn't have to worry about restoring original content to a file. But the hurdle there is that when I fire-up this javascript, it operates on the file that was opened when I clicked on the bookmarklet, without any obvious way to open up and then "accept all suggested changes" on the copied file.

这是我的代码(无法将Gdoc恢复到原始状态).在执行全局接受所有建议的更改"之前,关于应使用哪种方法使我的Gdoc达到原始状态的任何建议,将不胜感激:

Here's my code (which fails to restore the Gdoc to its original state). Any advice regarding what approach I should be using to get my Gdoc to the original state it was in before doing a global "accept all suggested changes" would be very much appreciated:

javascript:(function()  {sessionStorage.setItem("backup",document.getElementsByClassName("docos-icon-accept-suggestion-checkmark")); var d = document.getElementsByClassName("docos-icon-accept-suggestion-checkmark"); d = Array.prototype.slice.call(d); var cnt = 0;  d.forEach(function(n){ var e = document.createEvent("MouseEvents");  e.initEvent("click", true, false);  n.dispatchEvent(e,true); e = document.createEvent("MouseEvents");  e.initEvent("mousedown", true, false);  n.dispatchEvent(e,true); e = document.createEvent("MouseEvents");  e.initEvent("mouseup", true, false);  n.dispatchEvent(e,true); cnt++; }); alert('JUST ACCEPTED ALL SUGGESTED EDITS: ' + cnt.toString()); document.getElementsByClassName("docos-icon-accept-suggestion-checkmark").replaceChild(sessionStorage.setItem("backup")); alert('JUST FINISHED RESTORING TO ORIGINAL FILE');}) (); 

一些可能有用/相关的链接:

  • 编写google docs表单提交的脚本 (存在从打开的永久"文件运行Javascript的方式如何能够打开克隆的文档的问题;但是,该脚本可能可以毫无问题地复制和上传文档).
  • 使用JavaScript以编程方式编辑Google文档 (与我的问题很相关,但是答案暗示没有Google Apps脚本就无法更改Gdoc内容,这让我感到困惑,因为我确实能够接受建议的更改"并更改文档的外观.我尝试使用当前的javascript尝试使用Google Apps:我与打开的文档绑定在一起,无法以编程方式打开和操作其他文档.此外,我无法让Google Apps执行接受所有建议的脚本./em>
  • 如何克隆(和还原)DOM子树(我从中获得了使用.cloneNode的想法的参考文献之一,事实证明这对我的项目来说是另一个死胡同).
  • scripting a google docs form submission (has problem of how the Javascript running from the opened "permanent" file, would be able to open the cloned document -- the script could probably copy and upload the doc without problems, however).
  • Programmatically edit a Google doc with JavaScript (quite related to my question, but answers implied that one cannot change Gdoc content without Google Apps Script, which I find confusing because I definitely was able to "accept suggested changes" and change the apparent content of the document. I had the same problem with my attempts at Google Apps as I have with the current javascript: I am tied to the opened document and unable to open and manipulate other documents programmatically. Also, I was unable to get Google Apps to execute the script that accepts all suggestions).
  • How to clone (and restore) a DOM subtree (one of the references from which I got the idea of using .cloneNode, which proved to be another dead end for my project).

推荐答案

以下以书签形式编写的JavaScript可以实现上述所有目标,以防万一此解决方案可以帮助同样希望暂时接受所有建议的其他人"更改",然后将文档恢复为原始状态.如果您在执行此答案底部给出的Bookmarklet时显示Google文档,则遵循以下具体步骤及其完成方式.

The following JavaScript, written as a bookmarklet, achieves all of the above objectives, in case this solution might help others who similarly would like to temporarily "accept all suggested changes" in a Google Doc, and then revert the document back to the original state. The specific steps and how they were accomplished follows, if you have a Google Docs on display at the time that you execute the Bookmarklet given at the bottom of this answer.

  1. 以编程方式接受所有建议的更改. (来自 http://www.tcg的算法. com/blog/accept-all-bookmark-for-google-docs-suggestions/):

var d=document.getElementsByClassName("docos-accept-suggestion"); d = Array.prototype.slice.call(d); d.forEach(function(n){ var e = document.createEvent("MouseEvents");  e.initEvent("click", true, false);  n.dispatchEvent(e,true); e = document.createEvent("MouseEvents");  e.initEvent("mousedown", true, false);  n.dispatchEvent(e,true); e = document.createEvent("MouseEvents");  e.initEvent("mouseup", true, false);  n.dispatchEvent(e,true); });

  • 以编程方式导出纯文本(现在应该包含建议的更改):

  • Programmatically export plain text (which should now have suggested changes incorporated):

    window.open(window.location.href.replace("edit","export\?format=txt"));
    

  • 使用Google Doc和/或导出的纯文本文件执行操作(例如,我的文件包含原始LaTeX,因此我编译了该文件并获得了研究论文的pdf).

  • Do stuff with the Google Doc and/or with the exported plain text file (for example, my files had raw LaTeX, so I compiled the file and got a pdf of my research paper).

    将文档切换回其原始的pre-accept_all_suggested_changes状态.

    Switch the Doc back to its original, pre-accept_all_suggested_changes state.

    w=window.open(window.location.href.replace("edit","copy"));w.addEventListener('load',w.close(),true);alert('Check your download folder for the text file');setTimeout(function(){var a=document.getElementsByClassName("docs-icon-undo")[0]; for(var j=0; j<cnt; j++){e=document.createEvent("MouseEvents"); e.initEvent("click",true,false);a.dispatchEvent(e,true);e=document.createEvent("MouseEvents");e.initEvent("mousedown",true,false); a.dispatchEvent(e,true);e=document.createEvent("MouseEvents");e.initEvent("mouseup",true,false);a.dispatchEvent(e,true)}}
    

  • (其中cnt是上一步中接受的建议更改的数量.代码为您计算出cnt的值.)

    (where cnt is the number of suggested changes that were accepted in the previous step. The code figures out the value of cnt for you.)

    一起,功能性书签如下(其中,"[DO STUFF]"是您想要使用Google Doc或纯文本文件并结合了所有建议的更改的任何代码):

    Altogether, the functional bookmarklet is as follows (where "[DO STUFF]" is where you put in whatever code you want that utilizes the Google Doc or plain text file with all suggested changes incorporated):

    javascript:(function(){var d=document.getElementsByClassName("docos-accept-suggestion"); d = Array.prototype.slice.call(d); d.forEach(function(n){ var e = document.createEvent("MouseEvents");  e.initEvent("click", true, false);  n.dispatchEvent(e,true); e = document.createEvent("MouseEvents");  e.initEvent("mousedown", true, false);  n.dispatchEvent(e,true); e = document.createEvent("MouseEvents");  e.initEvent("mouseup", true, false);  n.dispatchEvent(e,true); });window.open(window.location.href.replace("edit","export\?format=txt")); [*DO STUFF*]; w=window.open(window.location.href.replace("edit","copy"));w.addEventListener('load',w.close(),true);alert('Check your download folder for the text file');setTimeout(function(){var a=document.getElementsByClassName("docs-icon-undo")[0];for(var j=0; j<cnt; j++){e=document.createEvent("MouseEvents"); e.initEvent("click",true,false);a.dispatchEvent(e,true);e=document.createEvent("MouseEvents");e.initEvent("mousedown",true,false); a.dispatchEvent(e,true);e=document.createEvent("MouseEvents");e.initEvent("mouseup",true,false);a.dispatchEvent(e,true)}},5000);})();
    

    该代码已在Chrome浏览器上经过测试.

    The code has been tested on a Chrome browser.

    这篇关于以编程方式临时更改Google文档,然后替换为预先更改的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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