Search code examples
javascriptfunctioncall

Transmitting a value from one function to another


This issue is asked already some times. But in the application of google apps script i can't solve this problem.

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

In function one I do a call to function two, where I need this value A:

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

Who can help me?


Solution

  • You can return value from function using return i; and obtain it in within one by 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;
        }
    }