简体   繁体   中英

How do I loop a function and each time insert different variables which I'm pulling from cells in my spreadsheet until I finish my range

I'm trying to run a function that will place a Twilio call for each row with different variables collected from each row.

Meaning if i have 10 rows selected, I want to place 10 calls, but each call has different variables that I pull from rows of cells which I then insert in the call parameters.

I know how to place a call using the row selected as the parameters but I can't figure out how to repeat for more rows if I select let's say a range of 10 rows with each call being unique.

    function getCc() {
      var range = SpreadsheetApp.getActiveRange();
      var row = range.getRow();
      var range2 = SpreadsheetApp.getActiveSheet().getRange("A"+row);
      return range2.getValue();
    }
    
    function getExp() {
      var range = SpreadsheetApp.getActiveRange();
      var row = range.getRow();
      var range2 = SpreadsheetApp.getActiveSheet().getRange("B"+row);
      return range2.getValue();
    }
    
    function getCvc() {
      var range = SpreadsheetApp.getActiveRange();
      var row = range.getRow();
      var range2 = SpreadsheetApp.getActiveSheet().getRange("C"+row);
      return range2.getValue();
    }
    
    function getAmount() {
      var range = SpreadsheetApp.getActiveRange();
      var row = range.getRow();
      var range2 = SpreadsheetApp.getActiveSheet().getRange("D"+row);
      return range2.getValue();
    }
    
    function getNum() {
      var range = SpreadsheetApp.getActiveRange();
      var row = range.getRow();
      var range2 = SpreadsheetApp.getActiveSheet().getRange("E"+row);
      return range2.getValue();
    }

    function makeCall() {
      var callUrl = "https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxxxxxxxxxxxx/Calls.json";
    
      var num = getNum();
      var amount = getAmount();
      var cc = getCc();
      var lastfour = cc.toString().substring(12);
      var exp = getExp();
      var cvc = getCvc();
        
      var payload = {
        "SendDigits" : "wwww1" + num + "#wwww" + amount + "#wwww" + cc + "#www" + exp + "#www" + cvc + "#wwwwwwwwwwwwwwwwwwwww2",
        "To": "+184xxxxxxxx",
        "From" : "+134xxxxxxxxx",
        "Url": "https://brasxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "Method": "GET"
      };
      
      var options = {
        "method" : "post",
        "payload" : payload
      };
      
      options.headers = {    
        "Authorization" : "Basic " + Utilities.base64Encode("ACxxxxxxxxxxxxxxxxxxxb:6xxxxxxxxxxxxxxxxxx")
      };
    
    var ui = SpreadsheetApp.getUi();
    var response = ui.alert('Proceed with Charge ?', 'Do you want to Charge Card \r\n Ending in '+lastfour+' for $'+amount+' ?', ui.ButtonSet.YES_NO);

if (response == ui.Button.YES) {
  UrlFetchApp.fetch(callUrl, options);
} 
  
}

How about this:

function getData() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Your Sheet Name');
  const sr=2;//start row
  const sc=1;//start column
  const rg=sh.getRange(sr,sc,sh.getLastRow()-sr+1,sh.getLastColumn()-sc+1);
  const vs=rg.getValues();
  vs.forEach(function(r,i){
    makeCall(r);
  })
}


function makeCall(row) {
  var callUrl = "https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxxxxxxxxxxxx/Calls.json";
  var num = row[4];
  var amount = row[3];
  var cc = row[0];
  var lastfour = cc.toString().substring(12);
  var exp =  row[1];
  var cvc = row[2];
  var payload = {
    "SendDigits" : "wwww1" + num + "#wwww" + amount + "#wwww" + cc + "#www" + exp + "#www" + cvc + "#wwwwwwwwwwwwwwwwwwwww2",
    "To": "+184xxxxxxxx",
    "From" : "+134xxxxxxxxx",
    "Url": "https://brasxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "Method": "GET"
  };
  var options = {
    "method" : "post",
    "payload" : payload
  };
  options.headers = {    
    "Authorization" : "Basic " + Utilities.base64Encode("ACxxxxxxxxxxxxxxxxxxxb:6xxxxxxxxxxxxxxxxxx")
  };
  var ui = SpreadsheetApp.getUi();
  var response = ui.alert('Proceed with Charge ?', 'Do you want to Charge Card \r\n Ending in '+lastfour+' for $'+amount+' ?', ui.ButtonSet.YES_NO);
  if (response == ui.Button.YES) {
    UrlFetchApp.fetch(callUrl, options);
  } 
}

Perhaps you can add a column to the spreadsheet to determine the call type from that you could use a switch statement to select the appropriate data for each call.

function placeCalls() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Call Sheet');
  const v=sh.getDataRange().getValues();
  v.forEach(function(r,i){
    let calltype=r[10];
    switch(calltype) { //you will have to figure out what ever type of structure fits your needs
      case 'exp':
        let params={calltype:r[10],param1:,param2:}
        break;
        let params=
      case 'cc':
        break;
      case 'cvc':
        break;
    }
    placeCall(params);
  })
}

function placeColl() {
  //write your call function here. 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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