如何将函数传递给函数?

时间:2014-03-25 02:46:06

标签: google-apps-script

在运行google电子表格的附加菜单中列出的功能之前,我必须检查天气是否存在某个属性。我不想为每个函数创建相同检查的副本,而是创建一个函数,我可以将该函数作为参数传递。我怎么能这样做?

以下是我的无效测试代码,但您可能会明白这一点。

function testRun(){
  //in practicality this would be an add-on menu
  test1Check('test1()');
}


function test1(){
  Logger.log("Function Ran");
}


function test1Check(functionToRun){
      var labels = PropertiesService.getDocumentProperties().getProperty('labels');
  var ui = SpreadsheetApp.getUi(); // Same variations.
  if (!labels) {
    var result = ui.alert(
      'You have not yet set up the page for gClassFolders',
      'Would you like to do this now?',
      ui.ButtonSet.YES_NO);
  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    setupGCF();
  } 

  } 
  else { 
    functionToRun;
  }
}

1 个答案:

答案 0 :(得分:0)

我必须从发送的参数中删除(),并将()添加到函数中的变量中。

function testRun(){

  test1Check(test1);
}


function test1(){
  Logger.log("Function Ran");
}


function test1Check(functionToRun){
      var labels = PropertiesService.getDocumentProperties().getProperty('labels');
  var ui = SpreadsheetApp.getUi(); // Same variations.
  if (!labels) {
    var result = ui.alert(
      'You have not yet set up the page for gClassFolders',
      'Would you like to do this now?',
      ui.ButtonSet.YES_NO);
  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    setupGCF();
  } 

  } 
  else { 
    functionToRun();
  }
}