Stack Overflow Asked on December 11, 2021
I was looking through the network tab to see if redo/undo data is sent to the server in Google Sheets, and it seems it is not — it seems it is local to the local user/device that is modifying the information.
What might the data structure look like to implement the undo/redo functionality? My guess is it would be a stack (i.e., array) with the last undo-able action pushed to the stack, something like this:
actions = [
action_1,
action_2
]
But what would the actual data structure look like for each of these actions? I suppose for a basic modification of a cell the text could be stored, but there are just so many possible things to do in Google Sheets, so it seems the ‘action’ could be almost infinitely complex.
I could understand like above. For this, how about this sample script? So please think of this as just one of several possible answers.
Please do the following flow for using the sample script.
Sheet1
, please change it to Sheet1
. In this sample script, as a test situation, the undo and redo functions for running to the sheet of Sheet1
in the active Spreadsheet. Please be careful this.installedOnEdit
.
installedOnEdit
as OnEdit event trigger.onOpen()
is run by the simple trigger. And offset
is cleared. And then, please open the script editor.logSpreadsheet
is created to the root folder. This is used as the log.Put manually "baz" to the cell "A1".
At this time, when you see the log Spreadsheet, you can see the following values to the cell "A1:A3". This is used for undo and redo function.
{"range":"A1","value":"foo","oldValue":""}
{"range":"A1","value":"bar","oldValue":"foo"}
{"range":"A1","value":"baz","oldValue":"bar"}
Run the function undo
at the script editor.
baz
to bar
.undo
at the script editor.
bar
to foo
.undo
at the script editor.
foo
to the empty. Because the initial situation is the empty.redo
at the script editor.
foo
to bar
.redo
at the script editor.
bar
to baz
.redo
at the script editor.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var prop = PropertiesService.getScriptProperties();
function onOpen() {
prop.setProperty("offset", "0");
}
function getSheet() {
var logId = prop.getProperty("logSpreadsheetId");
if (logId) {
return SpreadsheetApp.openById(logId).getSheets()[0];
}
throw new Error("Please edit a cell in the Spreadsheet. By this, the Spreadsheet for logging is created.");
}
// Undo function
function undo() {
var logsheet = getSheet();
var offset = Number(prop.getProperty("offset"));
var lastRow = logsheet.getLastRow();
if (lastRow + offset > 0) {
var log = logsheet.getRange(lastRow, 1).offset(offset, 0).getValue();
var obj = JSON.parse(log);
sheet.getRange(obj.range).setValue(obj.oldValue);
prop.setProperty("offset", offset - 1);
}
}
// Redo function
function redo() {
var logsheet = getSheet();
var offset = Number(prop.getProperty("offset")) + 1;
var lastRow = logsheet.getLastRow();
if (lastRow + offset <= lastRow) {
var log = logsheet.getRange(lastRow, 1).offset(offset, 0).getValue();
var obj = JSON.parse(log);
sheet.getRange(obj.range).setValue(obj.value);
prop.setProperty("offset", offset);
}
}
// Please set the installable OnEdit event trigger to this function.
function installedOnEdit(e) {
var logId = prop.getProperty("logSpreadsheetId")
var ss;
if (logId) {
ss = SpreadsheetApp.openById(logId);
} else {
ss = SpreadsheetApp.create("logSpreadsheet");
prop.setProperty("logSpreadsheetId", ss.getId());
}
var log = {range: e.range.getA1Notation(), value: "value" in e ? e.value : "", oldValue: "oldValue" in e ? e.oldValue : ""};
ss.getSheets()[0].appendRow([JSON.stringify(log)]);
}
Answered by Tanaike on December 11, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP