有没有一种方法可以使用Google Apps脚本在上一次编辑时打开文档? [英] Is there a way to use Google Apps Script to open a document at the last edit?

查看:64
本文介绍了有没有一种方法可以使用Google Apps脚本在上一次编辑时打开文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个越来越长的Google文档.我用它收集了关于不同项目的几乎所有想法,因此我希望它在我离开的任何地方都可以打开(不一定要在最后).-我需要可以在任何设备上随时访问它,这就是为什么它是Doc而不是Word文件的原因,尽管我知道如何在其中编写宏.-

I have a Google Doc that's getting kind of long. I use it to collect pretty much all my thoughts on different projects, so I'd like it to open back up wherever I left off (not necessarily at the end). - I need it readily accessible on whatever device I'm on, that's why it's a Doc and not a Word file, even though I know how to write the macro there. -

经过一些挖掘,似乎有一个针对工作表的编辑触发器,但没有针对文档的触发器,因此我一直在尝试获取基于时间的触发器,而不是将其作为最新编辑的最佳近似.我在文档的Code.gs中编写了以下内容,但它似乎不起作用.有什么建议吗?

After some digging, it looks like there's an edit trigger for sheets but not for docs, so I've been trying to get a time-based trigger instead as a best approximation of the most recent edit. I wrote the following in the doc's Code.gs, but it doesn't seem to work. Any suggestions?

function onOpen() { 
  deleteTrigger();
  createTrigger();
  DocumentApp.getActiveDocument().setCursor(PropertiesService.getDocumentProperties().getProperty('lastOpen')); 
}

function createTrigger() {
  ScriptApp.newTrigger('savePlace')
      .timeBased()
      .everyMinutes(1)
      .create();
} 

function savePlace() {
  PropertiesService.getDocumentProperties().setProperty('lastOpen', DocumentApp.getActiveDocument().getCursor());
}

function deleteTrigger() {

  // Loop over all triggers and delete them
  var allTriggers = ScriptApp.getProjectTriggers();

  for (var i = 0; i < allTriggers.length; i++) {
    ScriptApp.deleteTrigger(allTriggers[i]);
  }
}

(此外,我是否必须在对话框中为 lastOpen 指定脚本属性?如果这样,放置在其中的最佳值是什么.])

(Also, do I have to specify a script property for lastOpen in the dialog box? And what's the best value to put in there if so. [I'm really, really new to this])

推荐答案

尝试一下:

离开时只需保存您的书签.然后在打开文档时使用gotoBookMark.

Just save your bookmark when you leave. And the use gotoBookMark when you open up the document.

function saveBookMark() {
  var doc=DocumentApp.getActiveDocument();
  var cursor=doc.getCursor();
  var bookmark=doc.addBookmark(cursor);
  var ps=PropertiesService.getDocumentProperties();
  ps.setProperty('bookmarkid', bookmark.getId());
}

function gotoBookMark() {
  var doc=DocumentApp.getActiveDocument();
  var bookmarkid=PropertiesService.getDocumentProperties().getProperty('bookmarkid');
  var pos=doc.getBookmark(bookmarkid).getPosition();
  doc.setCursor(pos);
}

这将删除所有书签:

function clearBookMarks() {
  var doc=DocumentApp.getActiveDocument();
  var bkmarks=doc.getBookmarks().forEach(function(bm){bm.remove();})
}

这是菜单:

function menu() {
  DocumentApp.getUi().createMenu('MyMenu')
  .addItem('Save BookMark', 'saveBookMark')
  .addItem('Goto Bookmark', 'gotoBookMark')
  .addItem('Remove All Bookmarks', 'clearBookMarks')
  .addToUi();  
}

类书签

我以前从未真正使用过它,但我想可以花一点力气就可以从每个书签中摘录并提供一个复选框以选择它们,然后仅删除您不想要的那些并提供一个按钮紧挨着每一个,以便您随时可以返回到那个.

I've never actually used this before but I imagine with a little extra effort one could post an excerpt from each bookmark and provide a checkbox to select them and then only delete the ones you don't want and also provide a button next to each one so you could return to that one whenever you wish.

这是让我滚动的示例代码:

This was the sample code that got me rolling:

// Insert a bookmark at the cursor position and log its ID.
var doc = DocumentApp.getActiveDocument();
var cursor = doc.getCursor();
var bookmark = doc.addBookmark(cursor);
Logger.log(bookmark.getId());

之后,您可以使用代码完成功能找出可用的方法,然后在必要时参考手册.有时,代码完成会在文档之前进行更新,有时文档会首先进行更新,所以有时您不得不舍弃它.

After that you can use code completion to figure out what methods are available and then refer to the manual when necessary. It's interest sometimes code completion is updated before the document and sometimes the documentation gets updated first so you sometimes have to willing to wing it on your a bit.

我使用文档属性服务保存了最后一个书签ID,因此当您打开文档时,它总是转到最后一个书签.

I used the Document Properties Service to save the last bookmark id and so when you open up the document it always goes to the last bookmark.

PropertiesService

这篇关于有没有一种方法可以使用Google Apps脚本在上一次编辑时打开文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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