Open the Google spreadsheet with the cursor on the last working cell.

I would like for the cursor to step on the last working cell (to remember the last position) when opening any of my Google spreadsheets. Is it possible?

thank

+4
source share
2 answers

This can probably be written more briefly, but it will be the simplest idea, preserving your row and cell properties each time you edit, and then setting them to open. Go to the Property Service page for more information about which users can access these properties.

, , .

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);  
}  
+4

, 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));
  }
} 

:

  1. onCursoveMove(): . DocumentApp.getActiveDocument().getCursor() Position . , Position , (JSON.stringify Element ). , .

  2. onOpen: setCursor setCursor

. 2019 onClose() onExit() onCursorMove(). , HTMLService, TriggerBuilder . 6 .

0

All Articles