0
votes

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
} 
2
Did you write the functions?TheMaster
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
getRowNum function's name is undefinedTheMaster
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
Maybe, you can show me other way, how to get cell index from table by row "Name" and col "Name".Pavel Zakharenko

2 Answers

1
votes

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.
0
votes

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