简体   繁体   中英

Convert Python Script to Google Sheets Script

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)
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM