Reputation: 15
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?
Upvotes: 1
Views: 2246
Reputation: 201338
I believe your goal as follows.
Pass
when the value of column "B" is Yes
.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.
Pass
and Fail
from the cells "E5" and "E6", respectively?When above flow is converted to the Google Apps Script, it becomes as follows.
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);
}
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)
.
Upvotes: 1