I have a sheet with a column that contains a dropdown.The options in that dropdown are readible strings (idiot proof)But for further processing, these strings need to be converted to numbers.So on another sheet I have a range with two columns. First column contains my strings, the second their corresponding numbers.
What I need now is a script that will change the strings of the dropdown into numbers
So the script should:
- fire on edit (so when a user selects a string from the dropdown)
- lookup the string on the other sheet and return the corresponding number
- Change the value of the edited cell into that number.
I have a working code in excel (using named ranges), but I'm having trouble converting it to google sheets.Here is the code in excel:
Private Sub Worksheet_Change(ByVal target As Range)If Not Intersect(target, Range("C:C")) Is Nothing ThenRv = target.ValueSvc = Sheets("Named Ranges").Range("StoreViews").Rows.CountFor I% = 1 To Svc If Rv = Sheets("Named Ranges").Range("StoreViews").Item(I%, 2) Then target.Value = Sheets("Named Ranges").Range("StoreViews").Item(I%, 1)NextEnd IfEnd SubAnd here is the code I have in my script so far.
function onEdit() { const ss = SpreadsheetApp.getActiveSpreadsheet() const ws = ss.getActiveSheet() const mySheet = ss.getSheetByName("NewRedirects") //enter the actual sheet name const rangeSheet = ss.getSheetByName("StoreViewCombos")//only run if the edit was on the correct sheet if (ws.getName() !== mySheet.getName() ) { return }//only run if col C (3) was edited const targetCol = ws.getActiveCell().getColumnIndex() if ( targetCol !== 3) { return } var lastR = getLastDataRow(sheet,"C")for (var counter = 0; counter <= lastR; counter = counter + 1) { ;}(The getLastDataRow function is a separate function that gives me the lastrow in column C)
What's missing is the line of code that changes the edited value to the number.Can anybody help me out?
My sheet is here:https://docs.google.com/spreadsheets/d/1ud_WkHSBt0iGyBMnPNtcvwsWTa9dOIXSm_fCa0TQUcg/edit?usp=sharing