0

i need some help from someone who is more experience than me. I've the following formula

=WENNFEHLER(SVERWEIS($B$3;$B6:C;{2};0))

and the following script

function copyIntoCell() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('C3').activate();
  spreadsheet.getRange('A3').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};

How is it possible to add the formula into the script and also make it "onEdit" when a name entered into B3 it should auto insert the number into C3 from B6:C when B3 match with the database.

Also is it possible to autocomplete when I enter a word into B3 it suggest me the names from the database with the word I tipped in? This one is not important but would be nice.

Here is the example

Thanks for any help and idea I can get to complete what I looking for.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
mKeey
  • 133
  • 1
  • 13
  • I am not aure if I understand your question correctly. Do you want to copy a value from A3 to C3 or do you want to assign a formula to cell C3 (when B3 is being edited)? Do you want to do this only for row 3 or for any row for the respective columns? – ziganotschka Jul 02 '20 at 08:50
  • I want to get this Formula "=IFERROR(VLOOKUP($B$3;$B6:C;{2};0))" into a onEdit trigger event. When I enter a name into B3, the script should search for the name in the database and get the Value of ID and insert it into C3. if there is no name in the database what have been entered into B3 it should do nothing, so I can enter on my own a ID into C3 to add it into the database. the onEdit funtcion should be only for B3 not for any other Column – mKeey Jul 02 '20 at 09:04

1 Answers1

2

Combine script and formula as following:

  • Check if the edit was performed in column B
  • Retrieve the active row
  • setFormula() to assign your formula to the active row in column C
  • If the entered name is not found in the database and the formula returns the error "#NAME?" - delete the formula again
function onEdit(e) {
  //check if edit takes place in the second column (B)
  if(e.range.getA1Notation() =="B3"){
    //proceed
    var spreadsheet = SpreadsheetApp.getActive();
    var row = e.range.getRow();
    var formula = "=IFERROR(VLOOKUP($B$3,$B6:C,{2},0))";
    //set formula to active row in column C
    var cell = spreadsheet.getActiveSheet().getRange(row, 3);
    cell.setFormula(formula);
  }
};
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thanks for the work, but it is not working. I see the formula in C3 than it comes up #NAME? and it will be cleared. But the name is in the database for sure. Is it possible to make (e.range.getColumn() ==2) , change to B3? Maybe you should check the document I added into the first post how it looks like if you didn't already you can edit the document btw. – mKeey Jul 02 '20 at 09:46
  • Error : Unknown Function everytime. Is it possible to get otherwise working? Maybe without the formula and make it "hardcoded" withing the script? – mKeey Jul 02 '20 at 10:01
  • You can change `if(e.range.getColumn() ==2)` to `if(e.range.getA1Notation() =="B3")` if you only want to make it work on cell B3. I see, I receive `"#NAME?"` because of the conflict betweent he English and German version of the formula. If I understand correctly, if the name is not found - C3 will be empty anyway, so there is no need to delete the formula. I will update my answer. Have a look if this is what you meant. Depending on your settings - if your script expects the English version of the formula, it would be `=IFERROR(VLOOKUP($B$3,$B6:C,{2},0))`. – ziganotschka Jul 02 '20 at 10:01
  • 1
    Works fine now, I changed the language to english. The German one isn't working i don't know why. Anyways thank you :) – mKeey Jul 02 '20 at 10:09
  • Seems to be a [bug](https://issuetracker.google.com/67426320). – ziganotschka Jul 02 '20 at 10:11