简体   繁体   中英

Google Sheets - Script to change cell value based on several cells values

I have been searching for a while and trying to work together a script from various answered topics that will allow me to adjust an adjacent cells content based on the data entered. I cannot seem to get it to work properly and need some help steering the ship the right direction. Here is what I am trying to accomplish:

--If the value of cell A2:A is a six digit number AND the value of cell D2:D (same row) is "MATCH" then the value for cell B2:B should be set to "ANN" --If the value of cell A2:A is a six digit number AND the value of cell D2:D (same row) is "NO MATCH" then the value for cell B2:B should be set to "ANN" and a drop-down data validation list of ['ANN','RNW'] populate WITH the default value of the list set to "ANN" --If the value of cell A2:A has a length of seven or greater characters then a drop-down data validation list of ['1DY','RNW','NEW'] populate WITH the default value of the list set to "1DY"

Is it even possible to set the value of a data validation cell to a specific, default value? This is important as when the user is entering data they will more than likely accept the default value. If they don't want the default value then they can select a value from the drop-down list.

I built a test sheet which shows the what the sheet should look like when data is filled out in column A and the associated values in column B.

My test is here: https://docs.google.com/spreadsheets/d/1p8sq63S-vSU1FKFLjtr2ZypItN5viXotoZL0Ki2PoQM/edit?usp=sharing

Here is the cobbled together script I was attempting to build (I too find it funny). This is my first attempt to right a Google Script to run on a spreadsheet.

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var aSheet = ss.getActiveSheet();
  var aCell = aSheet.getActiveCell();
  var aColumn = aCell.getColumn();
  var aRow = aCell.getRow();
  //var licenseStatus = aSheet.getRange(aRow, aColumn+9).getValue();

 // The row and column here are relative to the range
 // getCell(1,1) in this code returns the cell at B2, B2
  var licenseTypeCell = aSheet.getRange(aRow, aColumn+1);

  if (aColumn == 1 && aSheet.getName() == 'Onsite') {
    if (isnumber(aCell) && (len(aCell) <= 6)) {
      var rule = SpreadsheetApp.newDataValidation().requireValueInList(['ANN','RNW']).build();
      licenseTypeCell.setValue("ANN");
      licenseTypeCell.setDataValidation(rule);
    } else {
      var rule = SpreadsheetApp.newDataValidation().requireValueInList(['1DY','RNW','NEW']).build();
      licenseTypeCell.setValue("1DY");
      licenseTypeCell.setDataValidation(rule);
    }
  }
}

Any help/guidance would be greatly appreciated.

You are on the right track, few minor changes. Below you will find some new function to be used in your code.

1) getValue() You get your cell using var aCell = aSheet.getActiveCell() ie the cell that was edited. But to get the value of the cell you will need to do the following aValue = aCell.getValue()

2) isNaN() To check if the aValue (as determined above) is a number or not. You will use a function called isNaN(aValue) . Google script uses javascript platform and hence we need to use functions from javascript . This is different from an inbuilt function you use in a google spreadsheet. It returns True if the value is Not A Number (NAN). Hence, we use a not operator( ! ) to flip the return value, like so

if(!isNaN(aValue))

3) Number of digits There is no len function in google scripts, hence to determine if the number is 6 digits long you can do the following

 if(aValue < 1000000) 

Your final code will look something like this:

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var aSheet = ss.getActiveSheet();
  var aCell = aSheet.getActiveCell();
  var aColumn = aCell.getColumn();
  var aRow = aCell.getRow();
  //var licenseStatus = aSheet.getRange(aRow, aColumn+9).getValue();

 // The row and column here are relative to the range
 // getCell(1,1) in this code returns the cell at B2, B2
  var licenseTypeCell = aSheet.getRange(aRow, aColumn+1);
  var aValue = aCell.getValue()
  if (aColumn == 1 && aSheet.getName() == 'Main') {
    if (!isNaN(aValue) && aValue < 1000000) {
      var matchCell = aSheet.getRange(aRow, aColumn+3).getValue()
      //The above gets value of column D (MATCH or NO MATCH)
      if(matchCell == "MATCH"){ //Check if Col D is MATCH
        licenseTypeCell.setValue("ANN");
      }
      else{
      var rule = SpreadsheetApp.newDataValidation().requireValueInList(['ANN','RNW']).build();
      licenseTypeCell.setValue("ANN");
      licenseTypeCell.setDataValidation(rule);
      }
    } else {
      var rule = SpreadsheetApp.newDataValidation().requireValueInList(['1DY','RNW','NEW']).build();
      licenseTypeCell.setValue("1DY");
      licenseTypeCell.setDataValidation(rule);
    }
  }
}

Also, note the addition of the following lines to check for col D Value

var matchCell = aSheet.getRange(aRow, aColumn+3).getValue()
//The above gets value of column D (MATCH or NO MATCH)
if(matchCell == "MATCH"){ //Check if Col D is MATCH
 licenseTypeCell.setValue("ANN");
}

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