Home > Back-end >  Run Google appscript on change of dropdonw
Run Google appscript on change of dropdonw

Time:11-17

I am very new to Google Appscript and support of this community will really be appreciated. Using different posts on the Stakeoverflow community I had designed an appscript to copy and paste the data from one worksheet to another and this is working fine (the data of worksheet named Copy is paste in worksheet named Paste). I had also created a worksheet named Trigger and in that I had created a dropdown in cell A1, now I want my script to run every time when the dropdonw in this sheet is changed. I had checked the different solutions on this community but as am new to appscirpt could not design a perfect solution to my case. Below is the link to the sheet and my current script.

https://docs.google.com/spreadsheets/d/1xy5eN8_PHXi9RPWK_EKg99dylaDjQ9lcilrSW0GP4B0/edit#gid=0

function Referesh() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");


var source = copySheet.getRange(2,1,copySheet.getLastRow(),2);
var rub = copySheet.getRange(2,1,copySheet.getLastRow(),2);

var destination = 
pasteSheet.getRange(pasteSheet.getLastRow() 1,2,copySheet.getLastRow(),2);


source.copyTo((destination), {contentsOnly: true});


rub.clearContent();
console.log(pasteSheet.getLastRow());
}

Any help on above will be appreciated.

CodePudding user response:

I believe your goal is as follows.

  • You want to automatically run your script by changing the dropdown list of the cell "A1" of "Trigger" sheet.

In this case, how about using the OnEdit simple trigger as follows?

Modified script:

Please copy and paste the following script to the script editor of Spreadsheet and save the script. When you use this script, in this case, please change the dropdown list of the cell "A1" of "Trigger" sheet to "Run". By this, the script is run.

function onEdit(e) {
  var sheetName = "Trigger";
  var runScript = "Run";
  var { range, source, value } = e;
  var sheet = range.getSheet();
  if (sheet.getSheetName() != sheetName || range.columnStart != 1 || range.rowStart != 1 || value != runScript) return;
  var ss = source;
  var copySheet = ss.getSheetByName("Copy");
  var pasteSheet = ss.getSheetByName("Paste");
  var source = copySheet.getRange(2, 1, copySheet.getLastRow(), 2);
  var rub = copySheet.getRange(2, 1, copySheet.getLastRow(), 2);
  var destination = pasteSheet.getRange(pasteSheet.getLastRow()   1, 2, copySheet.getLastRow(), 2);
  source.copyTo((destination), { contentsOnly: true });
  rub.clearContent();
  console.log(pasteSheet.getLastRow());

  range.setValue("Refresh"); // By this script, the value of "Run" is changed to "Refresh".
}

Note:

  • It seems that there are 2 values of Run and Refresh in your dropdown list. Unfortunately, I couldn't know the trigger value. So, in this sample script, when the dropdown list is changed to Run, the script is run by the simple trigger. If you want to change this, please modify the above script.

  • When you run directly onEdit with the script editor, an error occurs because of no event object. So, please be careful about this.

  • If you want to ignore the trigger value, I think that the following script might be able to be used. In this case, the dropdown list is changed, the script is run.

      function onEdit(e) {
        var sheetName = "Trigger";
        var { range, source } = e;
        var sheet = range.getSheet();
        if (sheet.getSheetName() != sheetName || range.columnStart != 1 || range.rowStart != 1) return;
        var ss = source;
        var copySheet = ss.getSheetByName("Copy");
        var pasteSheet = ss.getSheetByName("Paste");
        var source = copySheet.getRange(2, 1, copySheet.getLastRow(), 2);
        var rub = copySheet.getRange(2, 1, copySheet.getLastRow(), 2);
        var destination = pasteSheet.getRange(pasteSheet.getLastRow()   1, 2, copySheet.getLastRow(), 2);
        source.copyTo((destination), { contentsOnly: true });
        rub.clearContent();
        console.log(pasteSheet.getLastRow());
      }
    

Reference:

  • Related