Home > Software design >  Creating a custom formula with array having multiple arguments in columns with different data
Creating a custom formula with array having multiple arguments in columns with different data

Time:05-28

Up to this point I've managed to get it to work for a single argument (body) but without the second argument (photoUrl):

function SendTelegram(body,photoUrl) {
  if (body.map) {
    var response = body.map(function(b) {return SendTelegram(b);});
  } else {
    var response = UrlFetchApp.fetch(
      "https://api.telegram.org/bot"   'AAA'   
        "/sendPhoto?caption="   encodeURIComponent(body)   
          "&photo="   encodeURIComponent(photoUrl)   
            "&chat_id="   'BBB'   
              "&disable_web_page_preview=true&parse_mode=HTML"
    );
  }
}

I'm not able to understand how I should proceed to work with this two different arguments, for example, the function in the spreadsheet would be:

=ARRAYFORMULA(SendTelegram(A1:A,B1:B))

In my attempts when I try to add the second argument, it always uses the first row value in all calls, it doesn't follow the array one by one.

CodePudding user response:

In your script, at SendTelegram(b) of var response = body.map(function(b) {return SendTelegram(b);});, the 2nd argument is not set. By this, at 2nd loop, photoUrl is not declared. I thought that this might be the reason of your issue.

And, in your script, I thought that it might be required to return the response value. So, how about the following modification?

Modified script:

function SendTelegram(body, photoUrl) {
  if (body.map) {
    return body.map((b, i) => SendTelegram(b, photoUrl[i]));
  } else if (body && photoUrl) {
    return UrlFetchApp.fetch(
      "https://api.telegram.org/bot"   'AAA'  
      "/sendPhoto?caption="   encodeURIComponent(body)  
      "&photo="   encodeURIComponent(photoUrl)  
      "&chat_id="   'BBB'  
      "&disable_web_page_preview=true&parse_mode=HTML", { muteHttpExceptions: true }
    ).getContentText();
  }
  return null;
}

Note:

  • This is a simple modification. Because I cannot understand your expected value from the URL. So, please modify the script for your situation.
  • Related