0

This code doesn't work:

const files = [ 'file-id0', 'file-id1' ].map(DriveApp.getFileById)

with the exception Exception: The parameters (String,number,number[]) don't match the method signature for DriveApp.getFileById.

Although this works fine:

const files = [ 'file-id0', 'file-id1' ].map(id=>DriveApp.getFileById(id))

What's the difference of these codes? I cannot understand why the former doesn't work.

Note that DriveApp#getFileById takes only one argument as described in the document. I feel the exception being weird though.

aiotter
  • 21
  • 3
  • If you're going to use map then the first parameter is the element as shown [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) which in this case is a string and not the callback function. The second statement works because you have provided the variable to hold the element which could have been anything [ 'fileid1','fileid2'].map(x => DriveApp.getFileById(x) – Cooper Nov 06 '21 at 12:41
  • If you wish a more erudite answer then include the javascript tag and I'm sure you will get an answer that more than adequately explains the problem – Cooper Nov 06 '21 at 12:48
  • See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#tricky_use_case – TheMaster Nov 06 '21 at 14:30
  • @TheMaster Why do you think this case is related to the case of `parseInt`? – aiotter Nov 06 '21 at 16:18
  • getFileById takes only one argument and which is a string. https://developers.google.com/apps-script/reference/drive/drive-app#getFileById(String) – aiotter Nov 06 '21 at 16:18
  • @aiotter And yet the script is passing 3 parameters are passed to it: `String,number,number[]` === id, index and the array itself – TheMaster Nov 06 '21 at 16:19
  • Here's a simple way to check to check if DriveApp.getFileById is compliant with multiple params: Does this throw a error? ``DriveApp.getFileById(id,1)`` – TheMaster Nov 06 '21 at 16:28
  • `DriveApp.getFileById(id,1)` throws a exception `Exception: The parameters (String,number) don't match the method signature for DriveApp.getFileById.` It cannot accept 2 params. – aiotter Nov 06 '21 at 17:06
  • I don't know why `DriveApp.getFileById()` is forced to get 3 params. It should take only 1 param. If so, 2 code snippets I wrote above should get the same result. – aiotter Nov 06 '21 at 17:09
  • @TheMaster I don't think this is a duplicate of `parseInt` question. `parseInt` takes 3 params, whereas DriveApp.getFileById somehow cannot take more than 2 params. Usually functions of JS with 1 param can take more than 2 and throw away unnecessary ones. I don't know why it cannot take them, and that is my question. – aiotter Nov 06 '21 at 17:23
  • @aiotter Issue is the same. **`.map` always sends 3 arguments**. `parseInt()` accepts those 3 arguments, but returns unexpected result. `getFileById()` does not accept more than 1 parameter and results in a error. Some functions can accept more params like `parseInt()`. Some functions can accept more params, but ignore all the rest except the 1st like `Number()`. Some functions like `getFileById()` cannot accept more than the expected one parameter `id`. That's how they're designed. Your question asked why? The answer for `parseInt` sufficiently answers that - " That map sends 3 params". – TheMaster Nov 06 '21 at 17:40
  • Your question asked "why the former is different from latter"? The answer for `parseInt` sufficiently answers that - "That map sends 3 params, unless explicitly specified". If you feel it's different, [Edit] your question to show why you think it's different and other members may vote to reopen, if they agree with you. – TheMaster Nov 06 '21 at 17:47

0 Answers0