Google Apps Script for 2 Dynamic Dropdowns from 2 separate tables - google-apps-script

I have the following Google Apps onEdit Script which looks up the table in columns A:D in my 'DropDown Lists' sheet. This works for the dropdown in column C of the 'HISOP Training' sheet when the Branch is selected in the dropdown in column A of the same sheet:
//Dynamic Dropdown for HISOP Training
{
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var tablists = 'Dropdown Lists';
var tabValidation = 'HISOP Training';
var ss = spreadsheet.getActiveSheet();
var datass = spreadsheet.getSheetByName(tablists);
var activeCell = ss.getActiveCell();
if (
activeCell.getColumn() == 1 &&
activeCell.getRow() > 1 &&
ss.getSheetName() == tabValidation
) {
activeCell
.offset(0, 2)
.clearContent()
.clearDataValidations();
var base = datass.getRange(2, 1, 1, 5).getValues();
var baseIndex = base[0].indexOf(activeCell.getValue()) + 1;
Logger.log(baseIndex);
if (baseIndex != 0) {
//Dynamic dropdown for 'Employee'
var validationRange = datass.getRange(6, baseIndex, 150);
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 2).setDataValidation(validationRule);
}
}
if (ss.getSheetName() == tabValidation) {
var lock = LockService.getScriptLock();
if (lock.tryLock(0)) {
autoid_(ss);
lock.releaseLock();
}
}
}
I now also want the dropdown in column D of the 'HISOP Training' sheet to select from the range BO:BR of the 'Dropdown List' sheet when the Branch in Column A of the 'HISOP Training' sheet is selected but I don't know how to write the script for this.
This is an image of my 'HISOP Training' sheet:
This is a link to a sanitised version of my spreadsheet:
Spreadsheet
I would really appreciate some help with this.
Final answer:
function myOnEdit(e){
var sheet = SpreadsheetApp.getActive().getSheetByName('HISOP Training');
var sheet2 = SpreadsheetApp.getActive().getSheetByName('Dropdown Lists');
var value = e.value;
var col = e.range.getColumn();
var row = e.range.getRow();
if(col==1 && e.range.getValue()=='New York' && row>2 && e.range.getSheet().getName()=='HISOP Training'){
// 2 is for dropdown D
var dropdownData = sheet2.getRange("A6:A150").getValues().flat();
var dropdownData2 = sheet2.getRange("BO4:BO150").getValues().flat();
var allowDropdown = sheet.getRange(row, col+2).clearDataValidations().clearContent();
var allowDropdown2 = sheet.getRange(row, col+3).clearDataValidations().clearContent();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
var rule2 = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData2, true).build();
allowDropdown.setDataValidation(rule);
allowDropdown2.setDataValidation(rule2);
}
if(col==1 && e.range.getValue()=='London' && row>2 && e.range.getSheet().getName()=='HISOP Training'){
var dropdownData = sheet2.getRange("B6:B150").getValues().flat();
var dropdownData2 = sheet2.getRange("BP4:BP150").getValues().flat();
var allowDropdown = sheet.getRange(row, col+2).clearDataValidations().clearContent();
var allowDropdown2 = sheet.getRange(row, col+3).clearDataValidations().clearContent();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
var rule2 = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData2, true).build();
allowDropdown.setDataValidation(rule);
allowDropdown2.setDataValidation(rule2);
}
if(col==1 && e.range.getValue()=='Paris' && row>2 && e.range.getSheet().getName()=='HISOP Training'){
var dropdownData = sheet2.getRange("C6:C150").getValues().flat();
var dropdownData2 = sheet2.getRange("BQ4:BQ150").getValues().flat();
var allowDropdown = sheet.getRange(row, col+2).clearDataValidations().clearContent();
var allowDropdown2 = sheet.getRange(row, col+3).clearDataValidations().clearContent();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
var rule2 = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData2, true).build();
allowDropdown.setDataValidation(rule);
allowDropdown2.setDataValidation(rule2);
}
if(col==1 && e.range.getValue()=='Tokyo' && row>2 && e.range.getSheet().getName()=='HISOP Training'){
var dropdownData = sheet2.getRange("D6:D150").getValues().flat();
var dropdownData2 = sheet2.getRange("BR4:BR150").getValues().flat();
var allowDropdown = sheet.getRange(row, col+2).clearDataValidations().clearContent();
var allowDropdown2 = sheet.getRange(row, col+3).clearDataValidations().clearContent();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
var rule2 = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData2, true).build();
allowDropdown.setDataValidation(rule);
allowDropdown2.setDataValidation(rule2);
}
}

Solution
From my interpretation of your question you want to be able to only select a dropdrown of values in columns C and D only if a specific value is selected in column A. To provide a solution to this question instead of providing a specific answer to your sheet case I have provided a general solution abstracting the question so that others with similar questions can easily interpolate the answer and use it as well. You will just need to adapt this answer to you case.
To achieve this I have set up a conditional on an onEdit function to catch changes in the sheet and if this conditional is met (in your case if the branch in column A is selected) then you will be able to select from C and D dropdowns. The following code that solves the issue has self explanatory comments:
function onEdit(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
// Get the value of the cell changed
var value = e.value;
// Get col and row of the cell changed to make sure it was in col A
var col = e.range.getColumn();
var row = e.range.getRow();
// If col A. Here you could also add another condition to the if for instance if the value on col A
// is equal to whatever value you want to select in the dropdown in A, then execute this
if(col==1 && e.range.getValue()=='VALUE 1'){
// Get the data for the range of the dropdown. Here you choose if you want to insert dropdowns for C and D
var dropdownData = sheet.getRange("C1:C5").getValues().flat();
// Get the cell we will let the dropdown go (clear all previous content and data validations)
// In your case these you be the columns D and
var allowDropdown = sheet.getRange(row, col+1).clearDataValidations().clearContent();
// Create rules for the respective dropdowns of C and D (in this case is just one rule because I am
// just using a single dropdown to exemplify how to do this
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
// Set the data validation
allowDropdown.setDataValidation(rule);
}
// SECOND RANGE. REPEAT FOR THE REST IF WE HAVE MORE SUB RANGES
if(col==1 && e.range.getValue()=='VALUE 2'){
var dropdownData = sheet.getRange("D1:D5").getValues().flat();
var allowDropdown = sheet.getRange(row, col+1).clearDataValidations().clearContent();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
allowDropdown.setDataValidation(rule);
}
}
And this is how this example would look like in the sheet:
As you seem to have difficulties understanding, I have adjusted the general case scenario to your specific situation and I have done for you both the right dropdowns for columns C and D. These are as follow:
function onEdit(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName('HISOP Training');
var sheet2 = SpreadsheetApp.getActive().getSheetByName('Dropdown Lists');
var value = e.value;
var col = e.range.getColumn();
var row = e.range.getRow();
if(col==1 && e.range.getValue()=='New York' && row>2 && e.range.getSheet().getName()=='HISOP Training'){
// 2 is for dropdown C
var dropdownData = sheet2.getRange("BO4:BO49").getValues().flat();
var dropdownData2 = sheet2.getRange("Z2:Z4").getValues().flat();
var allowDropdown = sheet.getRange(row, col+3).clearDataValidations().clearContent();
var allowDropdown2 = sheet.getRange(row, col+2).clearDataValidations().clearContent();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
var rule2 = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData2, true).build();
allowDropdown.setDataValidation(rule);
allowDropdown2.setDataValidation(rule);
}
if(col==1 && e.range.getValue()=='London' && row>2 && e.range.getSheet().getName()=='HISOP Training'){
var dropdownData = sheet.getRange("BP4:BP45").getValues().flat();
var dropdownData2 = sheet2.getRange("AA2:AA3").getValues().flat();
var allowDropdown = sheet.getRange(row, col+1).clearDataValidations().clearContent();
var allowDropdown2 = sheet.getRange(row, col+2).clearDataValidations().clearContent();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
var rule2 = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData2, true).build();
allowDropdown.setDataValidation(rule);
allowDropdown2.setDataValidation(rule);
}
if(col==1 && e.range.getValue()=='Paris' && row>2 && e.range.getSheet().getName()=='HISOP Training'){
var dropdownData = sheet.getRange("BQ4:BQ43").getValues().flat();
var dropdownData2 = sheet2.getRange("AB2:AB4").getValues().flat();
var allowDropdown = sheet.getRange(row, col+1).clearDataValidations().clearContent();
var allowDropdown2 = sheet.getRange(row, col+2).clearDataValidations().clearContent();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
var rule2 = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData2, true).build();
allowDropdown.setDataValidation(rule);
allowDropdown2.setDataValidation(rule);
}
if(col==1 && e.range.getValue()=='Tokyo' && row>2 && e.range.getSheet().getName()=='HISOP Training'){
var dropdownData = sheet.getRange("BR4:BR33").getValues().flat();
var dropdownData2 = sheet2.getRange("AC2:AC3").getValues().flat();
var allowDropdown = sheet.getRange(row, col+1).clearDataValidations().clearContent();
var allowDropdown2 = sheet.getRange(row, col+2).clearDataValidations().clearContent();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData, true).build();
var rule2 = SpreadsheetApp.newDataValidation().requireValueInList(dropdownData2, true).build();
allowDropdown.setDataValidation(rule);
allowDropdown2.setDataValidation(rule);
}
}
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

Related

columnEdit not applying to my range of columns

Script works really well but I have 12 columns and this script only works on A and B.
function onEditTwoWay(e) {
var ss = SpreadsheetApp;
var sheet = ss.getActiveSpreadsheet();
var sheet1 = sheet.getSheetByName("Dep1");
var sheet2 = sheet.getSheetByName("Dep2");
var cell = sheet.getActiveCell();
var value = cell.getValue();
var currentRow = cell.getRow();
var currentColumn = cell.getColumn();
var activeWorksheet = ["Dep1","Dep2"];
var columnEdit = [1,2,12];
if(activeWorksheet.indexOf(ss.getActiveSheet().getName()) > -1 &&
columnEdit.indexOf(currentColumn) > -1 && currentRow > 1) {
sheet1.getRange(currentRow, currentColumn).setValue(value);
sheet2.getRange(currentRow, currentColumn).setValue(value);
};
};
Try this:
function onEdit(e) {
const sh = e.range.getSheet();
var shts = ["Dep1", "Dep2"];
var cols = [1, 2, 12];
if (~shts.indexOf(sh.getName()) && ~cols.indexOf(e.range.columnStart) && e.range.rowStart > 1) {
shts.forEach(name => {
e.source.getSheetByName(name).getRange(e.range.rowStart, e.range.columnStart).setValue(e.value);
});
}
}
All columns need to be entered in columnEdit so changed from
columnEdit = [1,2,12] to columnEdit = [1,2,3,4,5,6,7,8,9,10,11,12]

Google Sheets script - code optimization - one script, two sheets, same workbook

I have one workbook with multiple sheets and I've been running some simple scripts on two of those sheets (those sheets are basically copies of one another).
Once script creates a dropdown based on the value entered in one of the cells, while the second script adds a timestamp based on when the new dropdown was edited.
Initially I had two onEdit functions running on the first sheet.
Then I created a copy of these functions, with small amendments, to run on another sheet(same workbook)
Given that I don't really know how to have a separate script per worksheet I now have a one script with 4 similar onEdit functions.
Since I'm a total newbie I'm sure there's a better, more efficient way to write this code.
I'd appreciate your help in optimizing this.
function onEdit(e) {
addTimestamp(e);
addTimestamp2(e);
Dropdown(e);
Dropdown2(e);
}
function addTimestamp(e) {
var startRow = 2;
var targetColumn = 10;
var ws = "Tracker1";
var row = e.range.get.Row();
var col = e.range.getColumn();
if(col === targetColumn && row >= startRow && e.source.getActiveSheet().getName() === ws) {
var currentDate = new Date();
e.source.getActiveSheet().getRange(row,11).setValue(currentDate);
}
}
function addTimestamp2(e) {
var startRow = 2;
var targetColumn = 10;
var ws = "Tracker2";
var row = e.range.get.Row();
var col = e.range.getColumn();
if(col === targetColumn && row >= startRow && e.source.getActiveSheet().getName() === ws) {
var currentDate = new Date();
e.source.getActiveSheet().getRange(row,11).setValue(currentDate);
}
}
function Dropdown(){
var tabLists = "StatusFlow";
var tabValidation = "Tracker1"
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);
var activeCell = ss.getActiveCell();
if(activeCell.getColumn() == 9 && activeCell.getRow() > 1 && ss.getSheetName() == tabValidation){
activeCell.offset(o,1).clearContent().clearDataValidations();
var makes = datass.getRange(1,1,1, datass.getLastColumn()).getValues();
var makeIndex = makes[0].indexOf(activeCell.getValue()) +1;
if(makeIndex !=0) {
var validationRange = datass.getRange(2, makeIndex, datass.getLastRow());
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).setAllowInvalid(false).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
}
}
}
function Dropdown2(){
var tabLists = "StatusFlow";
var tabValidation = "Tracker2"
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);
var activeCell = ss.getActiveCell();
if(activeCell.getColumn() == 9 && activeCell.getRow() > 1 && ss.getSheetName() == tabValidation){
activeCell.offset(o,1).clearContent().clearDataValidations();
var makes = datass.getRange(1,1,1, datass.getLastColumn()).getValues();
var makeIndex = makes[0].indexOf(activeCell.getValue()) +1;
if(makeIndex !=0) {
var validationRange = datass.getRange(2, makeIndex, datass.getLastRow());
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).setAllowInvalid(false).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
}
}
}
here is a more efficient way to replace the two addTimestamp functions:
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var activeSName = activeSheet.getName();
var row = e.range.get.Row();
var col = e.range.getColumn();
if ((activeSName == 'Tracker1' || activeSName == 'Tracker2') && col === 10 && row >= 2) {
activeSheet.getRange(row, 11).setValue(new Date());
}
}
You could give the two Dropdown functions a shot and see if you can do something similar. Nothing like learning-by-doing. :-D
Just be careful that in onEdit you are passing e to these Dropdown functions. But not using them in these functions.

Check if in cell Today's date then continue

I found a script on the Internet and am trying to alter it a little to fit my needs. But stumbled upon a problem. The script takes a list of bookmarks from the drop down menu and if you write OK in the next cell, then it transfers the line to the selected bookmark. I want to confirm today's date.
Copy of the sheet. Also here some diffeent.
var actionCol = 7;
var nameCol = 6;
https://docs.google.com/spreadsheets/d/1P9avWP8i5Z4KA4zt3EoUZNbgMKeXwnOYKsgCG2_vSLE/edit?usp=sharing
/**
* Moves row of data to another spreadsheet based on criteria in column 6 to sheet with same name as the value in column 4.
*/
function onEdit(e) {
// see Sheet event objects docs
// https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
var ss = e.source;
var s = e.range.getSheet();
var r = e.range;
// to let you modify where the action and move columns are in the form responses sheet
var actionCol = 10;
var nameCol = 9;
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
// -1 to drop our action/status column
var colNumber = s.getLastColumn()-1;
// if our action/status col is changed to ok do stuff
if (e.value == "ok" && colIndex == actionCol) {
// get our target sheet name - in this example we are using the priority column
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
// if the sheet exists do more stuff
if (ss.getSheetByName(targetSheet)) {
// set our target sheet and target range
var targetSheet = ss.getSheetByName(targetSheet);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
// get our source range/row
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
// new sheets says: 'Cannot cut from form data. Use copy instead.'
sourceRange.copyTo(targetRange);
// ..but we can still delete the row after
s.deleteRow(rowIndex);
// or you might want to keep but note move e.g. r.setValue("moved");
}
}
}
If I understand you correctly, you want to cut & paste the edited row if:
The edited column is G, and the value is a Date corresponding to today.
The corresponding name in column F refers to the name of an existing sheet.
In order to check that the two dates correspond to the same day, you can compare the values returned by getDate(), getMonth() and getFullYear(), as in this function (credits to Pointy):
function sameDay(d1, d2) {
return d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate();
}
Then, you can call this in your main function:
function onEdit(e) {
var ss = e.source;
var s = e.range.getSheet();
var r = e.range;
var value = r.getValue();
var actionCol = 7;
var nameCol = 6;
var rowIndex = r.getRow();
var colIndex = r.getColumn();
var colNumber = s.getLastColumn();
if (value instanceof Date && colIndex == actionCol) {
var today = new Date();
if (sameDay(value, today)) {
var targetSheetName = s.getRange(rowIndex, nameCol).getValue();
var targetSheet = ss.getSheetByName(targetSheetName);
if (targetSheet) {
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
sourceRange.copyTo(targetRange);
s.deleteRow(rowIndex);
}
}
}
}
Try it this way:
function onEdit(e) {
var ss = e.source;
var s = e.range.getSheet();
var r = e.range;
var actionCol = 10;
var nameCol = 9;
var rowIndex = r.rowStart;
var colIndex = r.columnStart;
var colNumber = s.getLastColumn()-1;
const dt=new Date(e.range.offset(0,n));//you add the column offset
const dtv=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
const td=new Date();
const tdv=new Date(td.getFullYear(),td.getMonth(),td.getDate()).valueOf();
if (e.value == "ok" && colIndex == actionCol && dtv==tdv) {
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
var tSh = ss.getSheetByName(targetSheet);
if (tSh) {
var targetRange = tSh.getRange(tSh.getLastRow()+1, 1, 1, colNumber);
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
sourceRange.copyTo(targetRange);
s.deleteRow(rowIndex);
}
}
}

Can I get help to fix the Apps Script code for Dependant Dropdowns?

I have dependent dropdowns in my Google Sheet per the link below:
Database
The dropdowns are in the 'Events/Incidents' sheet:
Columns D and E are dependent on column C. The is an image of the dropdown data from the 'Dropdown Lists' sheet:
The dropdown in column C of the 'Events/Incidents' sheet is data validation from 'Dropdown Lists' sheet A2 to F2. The selection in the column C dropdown will dictate the criteria in the columns D & E dropdowns and the data will be the same for both columns. This will be sourced from the 'dropdown lists' sheet from either A4 down, B4 down, and so on e.g. if 'Tauranga' is selected all of the names under Tauranga will be in the in the D & E dropdowns.
This is my onEdit code:
//Dependent Dropdowns for 'Event/Incidents' Sheet
{
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var tablists = 'Dropdown Lists';
var tabValidation = 'Events/Incidents';
var ss = spreadsheet.getActiveSheet();
var datass = spreadsheet.getSheetByName(tablists);
var activeCell = ss.getActiveCell();
if (
activeCell.getColumn() == 3 &&
activeCell.getRow() > 1 &&
ss.getSheetName() == tabValidation
) {
activeCell
.offset(0, 1)
.clearContent()
.clearDataValidations();
var base = datass.getRange(2, 1, 1, 5).getValues();
var baseIndex = base[0].indexOf(activeCell.getValue()) + 1;
Logger.log(baseIndex);
if (baseIndex != 0) {
//Dynamic dropdown for 'Event Recorded By'
var validationRange = datass.getRange(3, baseIndex, datass.getLastRow());
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
//Dynamic dropdown for 'Employee'
var validationRange2 = datass.getRange(4, baseIndex, datass.getLastRow());
var validationRule2 = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange2).build();
activeCell.offset(0, 2).setDataValidation(validationRule2);
}
}
if (ss.getSheetName() == tabValidation) {
var lock = LockService.getScriptLock();
if (lock.tryLock(0)) {
autoid_(ss);
lock.releaseLock();
}
}
I can't get this code to work and would appreciate some help.
Here is an example that you can implement in your onEdit function, this will create both drop-downs for columns D and E in each row depending on the value selected in column C:
var range = e.range;
var editedRow = range.getRow();
var spreadsheet = SpreadsheetApp.getActive();
var dropdownSheet = spreadsheet.getSheetByName("Dropdown Lists");
var eventsSheet = spreadsheet.getSheetByName("Events/Incidents");
var baseSelected = eventsSheet.getRange('C' + editedRow).getValue();
var column;
switch (baseSelected) {
case 'EBOP': column = 'A'; break;
case 'Tauranga': column = 'B'; break;
case 'Palmerston North': column = 'C'; break;
case 'Kapiti': column = 'D';
}
var startCell = dropdownSheet.getRange( column +'4');
var endCellNotation = startCell.getNextDataCell(SpreadsheetApp.Direction.DOWN).getA1Notation();
var ruleRange = dropdownSheet.getRange(startCell.getA1Notation() + ':' + endCellNotation);
var dropdown1 = eventsSheet.getRange('D' + editedRow);
var dropdown2 = eventsSheet.getRange('E' + editedRow);
var rule1 = SpreadsheetApp.newDataValidation().requireValueInRange(ruleRange).build();
var rule2 = SpreadsheetApp.newDataValidation().requireValueInRange(ruleRange).build();
dropdown1.setDataValidation(rule1);
dropdown2.setDataValidation(rule2);
I used the DataValidation [1] and DataValidationBuilder [2] classes.
[1] https://developers.google.com/apps-script/reference/spreadsheet/data-validation
[2] https://developers.google.com/apps-script/reference/spreadsheet/data-validation-builder

Matching values between 2 ranges continuously and pasting value in different col, if true

I have a google sheet with 2 tabs, BOMSheet & POProcess. I want to copy the PO Number from PO Process sheet by looking up the column B values of POProcess sheet (one by one continuously) in column D values of BOM Sheet and if matches then pasting the PO number in column M of the BOM sheet (for all values that are matched). here is the link of the sheet.
https://docs.google.com/spreadsheets/d/1MqYn2AjPncx-RvvyTS8Nj0ujpm9Lcd_ORJF9Wqbw6y0/edit?usp=sharing
I tried the code below but it's not working as its checking up the only one value from the PO Process sheet. Please help how to iterate the values from PO process sheet
function UpdateStatus() {
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ss = sss.getSheetByName('POProcess'); //replace with source Sheet tab name
var range = ss.getRange('C4'); //assign the range you want to copy
var data = range.getValues();
var range2 = ss.getRange('B8'); //value to look for to be replaced
var data2 = range2.getValues();
var sheet = sss.getSheetByName('BOMSheet');
var range3 = sheet.getRange('A:L');
var values = range3.getValues();
for (var i = 0; i < values.length; i++) {
if (values[i][3] == data2) {
values[i][12] = data;
}
}
range3.setValues(values);
}
You can use this function for only PO number.
function UpdateStatus() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pSheet = ss.getSheetByName('POProcess'); //replace with source Sheet tab name
var pNumber = pSheet.getRange('C4').getValue(); //assign the range you want to copy
var pRange = pSheet.getRange(8, 2, pSheet.getLastRow()-7, 1); //value to look for to be replaced
var pData = pRange.getValues().map(function(el) {
return el[0];
});
var sheet = ss.getSheetByName('BOMSheet');
var range3 = sheet.getDataRange();
var values = range3.getValues();
values.forEach(function(row) {
if (row[3] == '') return;
if (pData.indexOf(row[3]) > -1) {
row[12] = pNumber;
}
});
range3.setValues(values);
}
For mapping 'Qty Received' column also, try this.
function UpdateStatus() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pSheet = ss.getSheetByName('POProcess'); //replace with source Sheet tab name
var pNumber = pSheet.getRange('C4').getValue(); //assign the range you want to copy
var pRange = pSheet.getRange(8, 2, pSheet.getLastRow() - 7, pSheet.getLastColumn()-1); //values to map
var pData = {};
pRange.getValues().forEach(function(row) {
pData[row[0]] = row[6];
});
var sheet = ss.getSheetByName('BOMSheet');
var range3 = sheet.getDataRange();
var values = range3.getValues();
values.forEach(function(row) {
if (row[3] == '') return;
if (typeof pData[row[3]] != 'undefined') {
row[12] = pNumber;
row[13] = pData[row[3]];
}
});
range3.setValues(values);
}