打开 Google 电子表格,将光标放在最后一个工作单元格上
Open Google spreadsheet with the cursor on last working cell
我希望当我打开我的任何 google 电子表格时,将光标放在最后一个工作单元格上(以记住最后一个位置)。这可能吗?
谢谢
您可以创建一个在每次打开电子表格时运行的触发器。
这可能会写得更简洁,但这是最简单的想法,在每次编辑时保存您的行和单元格属性,然后将它们设置为打开。访问 Properties Service 页面以获取有关哪些用户可以访问这些属性的更多信息。
这将return到最后编辑的单元格,而不是光标所在的最后一个单元格。
工具 → 脚本编辑器并粘贴以下内容:
function onEdit(e) {
var ss = e.source
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell();
var row = cell.getRow();
var column = cell.getColumn();
var sheet_name = sheet.getSheetName();
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('row',row);
scriptProperties.setProperty('column',column);
scriptProperties.setProperty('sheet name',sheet_name);
}
function onOpen(e) {
var properties = PropertiesService.getScriptProperties();
var ss = e.source;
var sheet_name = properties.getProperty('sheet name');
var sheet = ss.getSheetByName(sheet_name);
var row = properties.getProperty('row');
var column = properties.getProperty('column');
var cell = sheet.getRange(row,column);
ss.setActiveSheet(sheet);
ss.setActiveRange(cell);
}
对于那些想要记住Google Docs中最后一个光标位置的人,这里有一种方法可以实现。
// record current cursor position
function onCursorMove() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var properties = PropertiesService.getScriptProperties();
// temporary string for indicating cursor position
var tempHinter = "3289asp"; // a random string
DocumentApp.getActiveDocument().getCursor().insertText(tempHinter);
var text = body.getText();
var index = text.search(new RegExp(tempHinter, "g"));
// delete temp string
body.editAsText().deleteText(index, index + tempHinter.length - 1); // end offset is inclusive
// store current cursor position as a script property
properties.setProperty("position", index);
}
// set current cursor to last-stored position in script properties
function onOpen() {
var properties = PropertiesService.getScriptProperties();
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var text = body.editAsText();
// get the cursor position stored by onCursorMove()
var position = properties.getProperty("position");
if (position !== null){
// set cursor to last-stored position
doc.setCursor(doc.newPosition(text, position));
}
}
解释:
onCursoveMove()
:记录当前光标在文档正文中的位置,并将整数位置保存在脚本属性中,供文档关闭和重新打开时访问。 DocumentApp.getActiveDocument().getCursor()
函数 returns 一个 Position
object to represent the cursor's position. However, because Position
uses element and offset to locate the cursor, it's very hard to store the element in script properties (JSON.stringify
doesn't work for Element
接口,因为它太复杂了)。为了保存光标位置,我在文档正文中存储了一个索引。
onOpen
:将当前光标重置到上次存储的位置,使用setCursor
函数
注意:截至 2019 年 1 月,应用程序脚本没有 onClose()
或 onExit()
触发器,因此 onCursorMove()
必须 定期 调用以更新当前光标位置。并且由于应用程序脚本不支持异步处理,您可以考虑使用 HTMLService, TriggerBuilder 或其他技术来规避。另外目前的执行时间限制是6分钟。
我希望当我打开我的任何 google 电子表格时,将光标放在最后一个工作单元格上(以记住最后一个位置)。这可能吗?
谢谢
您可以创建一个在每次打开电子表格时运行的触发器。
这可能会写得更简洁,但这是最简单的想法,在每次编辑时保存您的行和单元格属性,然后将它们设置为打开。访问 Properties Service 页面以获取有关哪些用户可以访问这些属性的更多信息。
这将return到最后编辑的单元格,而不是光标所在的最后一个单元格。
工具 → 脚本编辑器并粘贴以下内容:
function onEdit(e) {
var ss = e.source
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell();
var row = cell.getRow();
var column = cell.getColumn();
var sheet_name = sheet.getSheetName();
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('row',row);
scriptProperties.setProperty('column',column);
scriptProperties.setProperty('sheet name',sheet_name);
}
function onOpen(e) {
var properties = PropertiesService.getScriptProperties();
var ss = e.source;
var sheet_name = properties.getProperty('sheet name');
var sheet = ss.getSheetByName(sheet_name);
var row = properties.getProperty('row');
var column = properties.getProperty('column');
var cell = sheet.getRange(row,column);
ss.setActiveSheet(sheet);
ss.setActiveRange(cell);
}
对于那些想要记住Google Docs中最后一个光标位置的人,这里有一种方法可以实现。
// record current cursor position
function onCursorMove() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var properties = PropertiesService.getScriptProperties();
// temporary string for indicating cursor position
var tempHinter = "3289asp"; // a random string
DocumentApp.getActiveDocument().getCursor().insertText(tempHinter);
var text = body.getText();
var index = text.search(new RegExp(tempHinter, "g"));
// delete temp string
body.editAsText().deleteText(index, index + tempHinter.length - 1); // end offset is inclusive
// store current cursor position as a script property
properties.setProperty("position", index);
}
// set current cursor to last-stored position in script properties
function onOpen() {
var properties = PropertiesService.getScriptProperties();
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var text = body.editAsText();
// get the cursor position stored by onCursorMove()
var position = properties.getProperty("position");
if (position !== null){
// set cursor to last-stored position
doc.setCursor(doc.newPosition(text, position));
}
}
解释:
onCursoveMove()
:记录当前光标在文档正文中的位置,并将整数位置保存在脚本属性中,供文档关闭和重新打开时访问。DocumentApp.getActiveDocument().getCursor()
函数 returns 一个Position
object to represent the cursor's position. However, becausePosition
uses element and offset to locate the cursor, it's very hard to store the element in script properties (JSON.stringify
doesn't work forElement
接口,因为它太复杂了)。为了保存光标位置,我在文档正文中存储了一个索引。onOpen
:将当前光标重置到上次存储的位置,使用setCursor
函数
注意:截至 2019 年 1 月,应用程序脚本没有 onClose()
或 onExit()
触发器,因此 onCursorMove()
必须 定期 调用以更新当前光标位置。并且由于应用程序脚本不支持异步处理,您可以考虑使用 HTMLService, TriggerBuilder 或其他技术来规避。另外目前的执行时间限制是6分钟。