Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script Google脚本调用另一个函数以返回值_Google Apps Script - Fatal编程技术网

Google apps script Google脚本调用另一个函数以返回值

Google apps script Google脚本调用另一个函数以返回值,google-apps-script,Google Apps Script,我现在开始掌握GScript的初学者方法,但到目前为止,我只使用了一个函数。有人能告诉我如何“调用”另一个函数来检查某些内容,然后返回真或假。这是我的尝试(它最终会检查很多东西,但我只是检查一件事情开始…) 因此,基本上我只想让另一个函数检查某个值(在本例中是3大于2?),如果它是,则返回TRUE。从这一点上,我应该有知识,修改为真正的目的。我已经习惯了VisualBasic,所以我写了更多关于它的内容——我知道这是行不通的。有人能帮我转换一下,这样我就可以使用谷歌脚本了吗?这里有一个基本的示例

我现在开始掌握GScript的初学者方法,但到目前为止,我只使用了一个函数。有人能告诉我如何“调用”另一个函数来检查某些内容,然后返回真或假。这是我的尝试(它最终会检查很多东西,但我只是检查一件事情开始…)


因此,基本上我只想让另一个函数检查某个值(在本例中是3大于2?),如果它是,则返回TRUE。从这一点上,我应该有知识,修改为真正的目的。我已经习惯了VisualBasic,所以我写了更多关于它的内容——我知道这是行不通的。有人能帮我转换一下,这样我就可以使用谷歌脚本了吗?

这里有一个基本的示例,可以帮助你:

function petType(myPet){
    return myPet;
}

function mainFunctoin(){

  var newPet = petType("dog");

  if(newPet === "dog"){
     Logger.log("true");
  }else{
     Logger.log("false");
  }

}
执行mainFunction()。

如果将
petType
设置为“cat”,则返回false;但是,如果您将其设置为“dog”,它将返回true


如果有帮助,请告诉我。

您正在寻找带有返回语句的函数。假设您需要被调用函数从主函数获取一些输入:

function mainFunction() {
    //...
    var that = "some variable found above";
    //call other function with input and store result
    var result = otherFunction(that); 
    if (result) {
        //if result is true, do stuff
    }
    else {
        //if result is false, do other stuff
    }
}

function otherFunction(that) {
    var this = "Something"; //check variable
    return (this == that); 
    //(this == that) can be any conditional that evaluates to either true or false,
    //The result then gets returned to the first function
}
您也可以跳过分配结果变量,直接检查返回的条件,即:

if (otherFunction(that)) {
    //do stuff
}
else {do other stuff}

如果您需要我澄清任何语法,或者如果您还有任何问题,请告诉我。

google apps脚本基本上是javascript,了解一下可能会有所帮助,或者感谢您的帮助。很抱歉迟了回复,只是有机会再看一次。我的代码现在工作得很好。如果你有这个,出于某种原因,它不“总是”工作<代码>函数doGet(e){var ss=SpreadsheetApp.openByUrl(“https://docs.google.com/spreadsheets/d//edit#gid=0);var sheet=ss.getSheetByName(“Sheet1”);addUser(e,sheet);}函数addUser(e,sheet){var id=e.parameter.id;var name=e.parameter.name;sheet.appendRow([id,name]);}
if (otherFunction(that)) {
    //do stuff
}
else {do other stuff}