Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to undo changes made by a google apps script?

So I wonder what it takes to make changes made by google apps script to a document reversible. In particular I am working on a script that applies custom styles to selected elements from a document in Google Docs. It's not a hard thing to do. The problem is that the changes made by the script are not reflected in the history of the document and thus cannot be undone. There is no notion of a reversible editing session either as far as I can tell.

So is there a way to undo the changes made by a script?

function onOpen() {
  DocumentApp.getUi()
  .createMenu('Extras')
  .addItem('Apply code style', 'applyCodeStyle')
  .addToUi();
}

function applyCodeStyle() {
  var selection = DocumentApp.getActiveDocument().getSelection();
  if (selection) {
   var elements = selection.getSelectedElements();
   for (var i = 0; i < elements.length; i++) {
     var element = elements[i];

     // Only modify elements that can be edited as text; skip images and other non-text elements.
     if (element.getElement().editAsText) {
       var text = element.getElement().editAsText();

       // Bold the selected part of the element, or the full element if it's completely selected.
       if (element.isPartial()) {
         text.setBold(element.getStartOffset(), element.getEndOffsetInclusive(), true);
       } else {
         text.setBold(true);
       }
     }
   }
 }
}
like image 993
Trident D'Gao Avatar asked Jan 24 '26 06:01

Trident D'Gao


1 Answers

The closest I can imagine it to create a backup copy of your file in a specific folder every 5 minutes or so when you are modifying it so you have at least a copy of this doc version. Not ideal but it works...

Here is a piece of code that does it, starting from your code I just added the timer/copy stuff, you can try it by changing the folder ID.

EDIT : added a try/catch for first execution without error.

function applyCodeStyle() {
  var selection = DocumentApp.getActiveDocument().getSelection();
  try{
    var x = new Date().getTime()/60000-new Date(Utilities.jsonParse(ScriptProperties.getProperty('lastBKP'))).getTime()/60000 ;
  }catch(e){
    ScriptProperties.setProperty('lastBKP', Utilities.jsonStringify(new Date()));
    var x = 0
    }    
  Logger.log(x+' minutes')
  if (selection) {
    if(x > 5){
      var docId = DocumentApp.getActiveDocument().getId();
      DriveApp.getFileById(docId).makeCopy(DriveApp.getFolderById('0B3qSFd3iikE3NWd5TmRZdjdmMEk')).setName('backup_of_'+DocumentApp.getActiveDocument().getName()+'_on_'+Utilities.formatDate(new Date(),'GMT','yyyy-MMM-dd-HH-mm'));
      Logger.log("file copied because new Date().getTime()/3600-new Date(Utilities.jsonParse(ScriptProperties.getProperty('lastBKP'))).getTime()/3600 ="+x);
      ScriptProperties.setProperty('lastBKP', Utilities.jsonStringify(new Date()));
    }
    var elements = selection.getSelectedElements();
    for (var i = 0; i < elements.length; i++) {
      var element = elements[i];
      if (element.getElement().editAsText) {
        var text = element.getElement().editAsText();
        if (element.isPartial()) {
          text.setBold(element.getStartOffset(), element.getEndOffsetInclusive(), true);
        } else {
          text.setBold(true);
        }
      }
    }
  }
}
like image 199
Serge insas Avatar answered Jan 26 '26 21:01

Serge insas