简体   繁体   中英

Google Sheets - Script to fill cell IF column contains

I've searched endlessly and tried pasting pieces of code together but I can't seem to get it figured out.

I want to have a script that when executed (via button, I know how to do this part) will write "Pass" in the cells that contain "Yes" in column B

function storeValue() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // ss is now the spreadsheet the script is associated with
  var sheet = ss.getSheets()[0]; // sheets are counted starting from 0
  // sheet is the first worksheet in the spreadsheet
  var cell = sheet.getRange("D10"); 
  cell.setValue(Pass);
}

I was thinking it would be possible to have a vlook up condition inside the GetRange or is it more complicated than that?

example

I believe your goal as follows.

  • You want to put Pass when the value of column "B" is Yes .
  • You want to achieve this using Google Apps Script.

Unfortunately, I think that have a vlook up condition inside the GetRange cannot be directly achieved. So, in your case, as a simple case, I would like to propose the following flow.

  1. Retrieve the values from the column "B".
    • From your sample image, it seems that the values are put in the rows from 10 in the column "B".
    • Do you want to use the values of Pass and Fail from the cells "E5" and "E6", respectively?
  2. Create an array for putting to Spreadsheet from the retrieved values.
    • In this case, the if statement is used.
  3. Put the values from the row 10 in the column "D" Spreadsheet.

When above flow is converted to the Google Apps Script, it becomes as follows.

Modified script:

Please copy and paste the following script and run the function modified_storeValue() . As the test run, when you run the function of modified_storeValue() at the script editor, the script is run and the values are put to the column "D".

function modified_storeValue() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  
  // I added below script.
  //  1. Retrieve the values from the column "B".
  var values1 = "Pass";
  var values2 = sheet.getRange("B10:B" + sheet.getLastRow()).getValues();
  
  // 2. Create an array for putting to Spreadsheet from the retrieved values.
  var putValues = [];
  for (var i = 0; i < values2.length; i++) {
    if (values2[i][0] === "Yes") {
      putValues.push([values1]);
    } else {
      putValues.push([""]);
    }
  }

  // 3. Put the values from the row 10 in the column "D" Spreadsheet.
  sheet.getRange(10, 4, putValues.length, 1).setValues(putValues);
}

Note:

  • If you want to use the value of Pass retrieved from the cell "E5", please modify as follows.

    • From

       var values1 = "Pass";
    • To

       var values1 = sheet.getRange("E5").getValue();
  • When you run the script by clicking a button, please put an image and a drawing to Spreadsheet and assign the function to it. By this, the script can be run by clicking the button.

  • When the situation is changed from your sample image, this modified script might not work. So please be careful this.

  • If you want to use the worksheet by giving the sheet name, please modify ss.getSheets()[0] to ss.getSheetByName(sheetName) .

References:

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