简体   繁体   中英

Get the selected dropdown's value in spreadsheet in google script

新工作表 当前工作表

I have searched a lot about this but no results so far. My question is pretty simple, What are the possible ways to get a dropdown's(which i created with datavalidations) selected value without using onEdit() or any trigger in Google Script. I know this might be a pretty simple or even dumb question, but I am a novice in google sheets and script. Thanks for the help

Edit: These are the images of my sheet, I want to get the dropdown's value from the current sheet and then get the count of the selected element's frequency in new sheet and hence fill the count in the count column in the current sheet again.

Code:

function onOpen(){
  var ss = SpreadsheetApp;
  var currSheet = ss.getActiveSpreadsheet().getActiveSheet(); 
ScriptApp.newTrigger('myEdit')
.forSpreadsheet(currSheet)
.onEdit()
.create();
}

function myEdit(e){

  var activeCell = e.range;
  var val = activeCell.getValue();

  var wsName = activeCell.getSheet().getName();

  var r = activeCell.getRow();
  var c = activeCell.getColumn();

  if(wsName ==="Math" && r > 1)   
    returnFun(e.value);    
}

function returnFun(selectedDropdown){
  var ss = SpreadsheetApp;
  var currSheet = ss.getActiveSpreadsheet().getActiveSheet();

  var oss = SpreadsheetApp.openById(" Some id");
  var sheet = oss.getSheetByName(" Some Name "); 

// Here I check the selectedDropdown with a column of oss Spreadsheet 
// but the onEdit trigger keeps giving me error that I cant access that file
// this error only occurs when I use onEdit(), else the oss sheet works fine.

}

Explanation

Installable triggers inherit authorization from the process that created them . The onOpen() trigger you have is set to run on the current spreadsheet but at the same time it is creating an installable onEdit() trigger. This onEdit() trigger is later making use of another spreadsheet , for which you didn't authorize the permissions for.

Therefore, I suggest you create the onEdit() trigger by going to Current project's triggers -> Add trigger and choose the myEdit function for the trigger to run on. I also suggest you to skip the onOpen() trigger as you are only using it for creating the other trigger.

Snippet

function myEdit(e){
    var activeCell = e.range;
    var val = activeCell.getValue();
    var wsName = activeCell.getSheet().getName();
    var r = activeCell.getRow();
    var c = activeCell.getColumn();
    if (wsName === "Math" && r > 1)
        returnFun(e.value);
}

function returnFun(selectedDropdown){
    var ss = SpreadsheetApp;
    var currSheet = ss.getActiveSpreadsheet().getActiveSheet();
    var oss = SpreadsheetApp.openById(" Some id");
    var sheet = oss.getSheetByName(" Some Name ");

    // Here I check the selectedDropdown with a column of oss Spreadsheet 
    // but the onEdit trigger keeps giving me error that I cant access that file
    // this error only occurs when I use onEdit(), else the oss sheet works fine.
}

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM