0

The following code block is a python script with added Google Sheets script method title and arguments. I would like to use this code in a Google Sheets custom script.

function D20PROBS(INPUT1, INPUT2) {
    count=0
    for i in range(1,21):
        for j in range(1,21):
            if i+INPUT1 > j+INPUT2:
                count+=1
    print(count)
}
5
  • will this help youtube.com/watch?v=vISRn5qFrkM and that's not how you create a function in python Commented Nov 26, 2017 at 7:43
  • That seems like a very inefficient way to do what I need. It's adding a lot of extra steps. I feel like it'd be easier to just change the code into something that Google Sheets Scripts understand Commented Nov 26, 2017 at 7:46
  • Can I ask you about your question? Does "Google Sheets Scripts" mean "Google Apps Script"? Do you want to use the output (count) calculated by your python script as a custom function for Google Spreadsheet using Google Apps Script? If I misunderstand your question, I'm sorry. Commented Nov 26, 2017 at 8:07
  • The code you've posted is not python. Commented Nov 26, 2017 at 8:12
  • I want it to be a custom function in Google Sheets. You can add custom functions by using their code that's based on Javascript. Commented Nov 27, 2017 at 9:23

1 Answer 1

1

How about the following modifications?

Modification points :

  • for i in range(1,21): can be converted to for (var i=1; i<21; i++) {}
  • f i+INPUT1 > j+INPUT2: can be converted to if (i+INPUT1 > j+INPUT2) {}
  • print(count) was converted to return count for importing the result to the cell.

Modified script :

function D20PROBS(INPUT1, INPUT2) {
  var count = 0;
  for (var i=1; i<21; i++) {
    for (var j=1; j<21; j++) {
      if (i+INPUT1 > j+INPUT2) {
        count+=1;
      }
    }
  }
  return count;
}

Note :

  • You can use this function on Spreadsheet as a custom function. When you use this, please copy and paste this script to the script editor which is opened on Spreadsheet, and put =D20PROBS(number, number) to a cell.
  • INPUT1 and INPUT2 are necessary to be numbers.
Sign up to request clarification or add additional context in comments.

2 Comments

That's beautiful! Thank you very much, Tanaike! :) It worked like a charm.
@Mikitz06 I'm glad this was useful for you. Thank you, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.