简体   繁体   English

将Python脚本转换为Google表格脚本

[英]Convert Python Script to Google Sheets Script

The following code block is a python script with added Google Sheets script method title and arguments. 以下代码块是python脚本,其中添加了Google Sheets脚本方法的标题和参数。 I would like to use this code in a Google Sheets custom script. 我想在Google表格自定义脚本中使用此代码。

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++) {} for i in range(1,21):可以转换为for (var i=1; i<21; i++) {}
  • f i+INPUT1 > j+INPUT2: can be converted to if (i+INPUT1 > j+INPUT2) {} f i+INPUT1 > j+INPUT2:可以转换为if (i+INPUT1 > j+INPUT2) {}
  • print(count) was converted to return count for importing the result to the cell. print(count)转换为用于将结果导入到单元格的return count

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. 使用此脚本时,请将此脚本复制并粘贴到在Spreadsheet上打开的脚本编辑器中,然后将=D20PROBS(number, number)放入单元格中。
  • INPUT1 and INPUT2 are necessary to be numbers. INPUT1INPUT2必须是数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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