将值从一个函数传输到另一个函数

时间:2016-10-01 20:22:50

标签: javascript function call

有些时候会问这个问题。但在谷歌应用程序脚本的应用程序中我无法解决这个问题。

function two ()
{
  var bridgeclubs =   SpreadsheetApp.openById("1dfyI1jbz..........TVg4OoixKTz1");
  var bridgeclubs_sheet = bridgeclubs.getSheetByName("Sheet1");
  var data_bridgeclubs = bridgeclubs_sheet.getDataRange().getValues();
  var numRows = bridgeclubs_sheet.getDataRange().getNumRows();
  var sprshtname = SpreadsheetApp.getActiveSpreadsheet().getName();
  for (var i=1; i<=numRows; i++)
  {
   if (sprshtname == data_bridgeclubs[i][6])
   {
      var A = i;         
      break;
   }
  }
}

在函数一中,我调用函数二,我需要这个值A:

function one ()
{
two ();
//here I need this value A;
var C= 35 * A;for instance
}

谁能帮帮我?

2 个答案:

答案 0 :(得分:3)

您可以使用return i;从函数返回值,并在one var A = two();内获取它。

function two() {
    var bridgeclubs =   SpreadsheetApp.openById("1dfyI1jbz..........TVg4OoixKTz1");
    var bridgeclubs_sheet = bridgeclubs.getSheetByName("Sheet1");
    var data_bridgeclubs = bridgeclubs_sheet.getDataRange().getValues();
    var numRows = bridgeclubs_sheet.getDataRange().getNumRows();
    var sprshtname = SpreadsheetApp.getActiveSpreadsheet().getName();
    for (var i=1; i<=numRows; i++) {
        if (sprshtname == data_bridgeclubs[i][6]) {
            return i;
        }
    }
}

function one() {
    var C;
    var A = two();
    if (A) {
        C = 35 * A;
    }
}

答案 1 :(得分:0)

您可以使用全局变量。

var A;

function two() {
    var bridgeclubs =   SpreadsheetApp.openById("1dfyI1jbz..........TVg4OoixKTz1");
    var bridgeclubs_sheet = bridgeclubs.getSheetByName("Sheet1");
    var data_bridgeclubs = bridgeclubs_sheet.getDataRange().getValues();
    var numRows = bridgeclubs_sheet.getDataRange().getNumRows();
    var sprshtname = SpreadsheetApp.getActiveSpreadsheet().getName();
    for (var i=1; i<=numRows; i++) {
        if (sprshtname == data_bridgeclubs[i][6]) {
           A = i;         
           break;
        }
    }
}

function one() {
     two();
     var C= 35 * A;
}