0

UPD: Decided

It's script.google.com question.

I have Spreadsheet with information (row - data \ column - time), every cell inside is number of free "spaces".

Example:

Date\Time | 11:00:00 | 15:00:00 | 17:00:00
01.09.2019|    10    |    15    |    30
02.09.2019|    10    |    15    |    30
03.09.2019|    10    |    15    |    30
04.09.2019|    10    |    15    |    30

After inputting in Web form date and time - program should show me how much "space" is free.

  • Now have 3 steps after web form :
    1. Get row from form (data)
    2. Get column from form (time)
    3. Get cell value and return it in form (sheet.getRange(row, col).getValue();)

How to connect 3 functions together?

// Get information from form

<script>   
function getEmptySpace(){       
     var presentDate = document.getElementById("dt").value;
     var presentTime = document.getElementById("tm").value;    
     if(presentDate.length >= 10 && presentTime.length >= 8){
  google.script.run.withSuccessHandler(updateEmptySpaceAdult).getSpaceAdult(presentDate,presentTime);             
       }  
     }

     function updateEmptySpaceAdult(spaceAdult){      
        document.getElementById("anav").value = spaceAdult;        
     }
</script>

// My 3 functions, which sholud get cell value and return it in form

//Function 1 - getRowNum

function getRowNum(){
  var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Ticket");
  var data = sheet.getDataRange().getValues();
  var name = "19.12.2018";  //Here should be arg "presentDate" (.getSpaceAdult(presentDate,presentTime);)
  for(var i = 0; i<data.length;i++){
    if(data[i][0] == name){ //[0] because serch in column A      
      return i+1;
    }
  }
}

//Function 2 - getColNum

function getColNum() {
  var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Ticket");
  var time = "17:00:00"; //Here should be arg "presentTime" (.getSpaceAdult(presentDate,presentTime);)
  var range = sheet.getRange(2, 1, 1, sheet.getMaxColumns());
  var values = range.getValues();
  
  for (var row in values) {
    for (var col in values[row]) {
      if (values[row][col] == time) {
         return parseInt(col) + 1;
      }
    }
  }
}

//Function 3 - getCellValue

function getCellValue(){
var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Ticket");
var row = 8.0;
var col = 4.0;

var value = sheet.getRange(row, col).getValue();
}

// I think main Function is - .getSpaceAdult(presentDate,presentTime)

function getSpaceAdult(presentDate,presentTime){
*Call Function 1 - getRowNum() with arg "presentDate" and get RowNumber
*Call Function 2 - getColNum() with arg "presentTime" and get ColNumber
*Call Function 3 - getCellValue() by returns "getRowNum and getColNum" and show tabel cell value
In the end function getSpaceAdult = tabel cell value
} 
Marios
  • 26,333
  • 8
  • 32
  • 52
  • Did you write the functions? – TheMaster Sep 09 '19 at 07:16
  • Nope, but i test it. and it works separately. i can't understand how to connect some "for (var" cycles in one function. – Pavel Zakharenko Sep 09 '19 at 07:34
  • `getRowNum` function's `name` is `undefined` – TheMaster Sep 09 '19 at 09:01
  • Yes, cause function was testing and name was = 12.12.2019 (Example for test). Now its should be input from form (.getSpaceAdult(presentDate,presentTime); - name = presentDate). I can't understand how to collect 3 functions together. First function based on "presentDate" and calculate row, second on "presentTime" calculate colonum, and 3rd should take row and col than return index of cell. Sorry for my English. – Pavel Zakharenko Sep 09 '19 at 10:50
  • Maybe, you can show me other way, how to get cell index from table by row "Name" and col "Name". – Pavel Zakharenko Sep 09 '19 at 10:53
  • I'm having some trouble understanding what you're trying to do, are you trying to call the three functions together? was is it that you're trying to connect? – AMolina Sep 09 '19 at 12:09
  • Replace code. Now here is testing code, and my ideas about code construction. – Pavel Zakharenko Sep 09 '19 at 12:12
  • @AMolina , Hello. I am trying to connect 3 function together. But arguments (parameters - date and time) takes by "document.getElementById" from web form and lands in .getSpaceAdult(presentDate,presentTime) function. Then wanna take parameters - date and time. Use function getRowNum by parameter date (presentDate) for get number of Row next step.. Use function getColNum by parameter time (presentTime) for get number of Column next step.. Use function getCellValue by Row and Column parametes, and get cell Value. Place cell Value in Web form (getSpaceAdult = cellValue) – Pavel Zakharenko Sep 09 '19 at 12:23
  • You say `name` should be a argument. How about defining function `getRowNum` with argument `name`: `function getRowNum(name){.....}`? – TheMaster Sep 09 '19 at 12:37
  • Thank you. I'll try it. About getColNum with argument time: function getColNum(time){......}. But, how to make one function with "link" it to other? – Pavel Zakharenko Sep 09 '19 at 12:58

2 Answers2

1

To Read and practice:

Script Logic:

  • Declare all functions taking appropriate arguments
  • Remove duplicate calling of Spreadsheet service within each function.

Snippets:

function getRowNum(date, sheet){
  //receives two arguments date and sheet.
  //DO NOT REDCLARE date and sheet using `var`: var sheet =... or var date =
  //REMOVEDvar sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Ticket");
  var data = sheet.getDataRange().getValues();
  //REMOVEDvar name = "19.12.2018";  //Here should be arg "presentDate" (.getSpaceAdult(presentDate,presentTime);)
  for(var i = 0; i<data.length;i++){
    if(data[i][0] == date){ //[0] because serch in column A      
      return i+1;
    }
  }
}
function getColNum(time, sheet) {
  //receives two arguments time and sheet.
  //DO NOT REDCLARE date and sheet using `var`: var sheet =... or var time =
  /*Rest of your code except  declarations of time/ sheet
   *
   */
}
function getSpaceAdult(date, time){
  var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Ticket");
  var row = getRowNum(date, sheet); //Get row number passing on two arguments date and sheet
  var col = getColNum(time, sheet);
  var value = sheet.getRange(row, col).getValue();
  return value;//Added
}

Notes:

  • getValues() returns Object[][]: Some/All of received values maybe date objects. Date objects cannot be compared with ==. You may use getDisplayValues() instead to get dates as strings.
Community
  • 1
  • 1
TheMaster
  • 45,448
  • 6
  • 62
  • 85
0

it's alive

JavaScript

<script>

function getEmptySpace(){

     var presentDate = document.getElementById("dt").value;
     var presentTime = document.getElementById("tm").value;    
     if(presentDate.length >= 10 && presentTime.length >= 8){
     google.script.run.withSuccessHandler(updateEmptySpaceAdult).getSpaceAdult(presentDate,presentTime);     
     M.updateTextFields();       
       }  
     }

     function updateEmptySpaceAdult(spaceAdult){      
        document.getElementById("anav").value = spaceAdult;
        M.updateTextFields();
     }

</script>

Function.gs

function getSpaceAdult(presentDate,presentTime){
  var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Ticket");
  var row = getRowNumAdult(presentDate, sheet);
  var col = getColNumAdult(presentTime, sheet);
  var value = sheet.getRange(row, col).getValue();
  return value;  
}

function getRowNumAdult(presentDate, sheet){
  var data = sheet.getDataRange().getValues();  
  for(var i = 0; i<data.length;i++){
    if(data[i][0] == presentDate){ //[0] because serch in column A      
      return i+1;
    }
  }
}

function getColNumAdult(presentTime, sheet) {
  var range = sheet.getRange(2, 1, 1, sheet.getMaxColumns());
  var values = range.getValues();  
  for (var row in values) {
    for (var col in values[row]) {
      if (values[row][col] == presentTime) {         
        return parseInt(col) + 1; //parseInt(col) + 1 because serch in column A
      }
    }
  }
}