0

I must confess I'm new to apps script code.

I'm trying to make a function with two arguments, to give it two cells, and make it copy the value of the first cell into the second cell. The idea is to be able to use the function as a shortcut to make this operation again (and with maths sometimes) with many others.

I've made this, and tried a few variations.

function copy(case1, case2) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var c1 = parseInt(sheet.getRange(case1).getValue());
  sheet.getRange(case2).setValue(c1);
}
copy('K4', 'M4');

Wich results with "Exception: Argument cannot be null: a1Notation", copy @ Code.gs:3 (the line with var c1).

I'm probably not using it the good way. I did not find any subject speaking about this, but If I missed one please let me know. I thank you for any help you'll provide.

  • If you run `copy` function with the script editor, I think that such an error occurs. Because in this case, when `copy` is run, first, `copy('K4', 'M4');` is run, and then, `copy(case1, case2)` is run. at 1st run, no error occurs. But, at the 2nd run, an error occurs because `case1, case2` is not declared. When you want to run `copy('K4', 'M4');`, how about modifying `copy('K4', 'M4');` to `const sample = _ => copy('K4', 'M4');`? By this, when `sample` is run, your current issue can be removed. If this was not your expected direction, I apologize. – Tanaike Aug 28 '22 at 11:30
  • I'm... Sorry to ask xD I understand it's probably a problem with case1,case2 not declared, but I don't get the point with "const sample = _ => copy('K4', 'M4');". I know what a constant is, but not how it links with copy('K4','M4') at this point, or what does the _ here. If I'm supposed to write it this exact way, it did not work. – Djian Yamaneko Aug 28 '22 at 12:23
  • @DjianYamaneko You have to call the function `sample`. You are probably calling `copy` again. – TheMaster Aug 28 '22 at 13:21
  • Ok, new thing. Even following the question I'm supposed to duplicate, I got nothing better. But I just realised that despite the error message... The script makes what I want him to make. '-' (just saw your answer, gonna try sample, thx!) – Djian Yamaneko Aug 28 '22 at 13:26
  • @DjianYamaneko If you don't get it, you don't understand the "duplicate" answer. I added another duplicate and see this too: https://stackoverflow.com/questions/69572640/when-passing-a-variable-to-function-variable-length-return-a-typeerror – TheMaster Aug 28 '22 at 14:58
  • I did not get "sample" to work, sometimes apps script takes it, and sometimes not. (did I get it wrong? xD ) But I managed to understand that I had to make another function to tell the script to execute this one, which gives the specific arguments to copy. And it worked! I feel so nooby '-' thank you very much for your help and patience! If it can help another newbie like me, I used: ```function preset() { copy('K4', 'M4'); }``` And told the script to execute preset. – Djian Yamaneko Aug 28 '22 at 15:06

0 Answers0