Search code examples
dategoogle-sheetsgoogle-apps-script

Date is a text and cannot be coerced to a number


I'm trying to search for data from the Masterlist to the Data Entry Form but got an error saying Date is a text and cannot be coerced to a number, Column G consists of a formula where it will calculate number of days between two date (column E & F).

But when i tried to pull data from the Masterlist using Search button an error pop up mentioning Date is a text and cannot be coerced to a number (refer to Image 1)

Image 1: Error enter image description here

I already tried to format the column but the problem still persists as it converts the date to text not number when I tried to search the data. Below is the code used:

var SPREADSHEET_NAME = "Billing Masterlist"
var SEARCH_COL_year = 0;
var SEARCH_COL_hospital = 1;

function searchStr() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var formSS = ss.getSheetByName("Data Entry Form");

  var hospital = formSS.getRange("B7").getValue();
  var year = formSS.getRange("D7").getValue();

  var values = ss.getSheetByName("Billing Masterlist").getDataRange().getValues();
  var cont = 0;
  for (var i = 0; i < values.length; i++)

  {
    var row = values[i];

    if ((row[SEARCH_COL_year] == year && row[SEARCH_COL_hospital] == hospital ))

    {

      var s = (11 + cont).toString();
      formSS.getRange("B" + s).setValue(row[2]);
      formSS.getRange("E" + s).setValue(row[4]);
      formSS.getRange("F" + s).setValue(row[5]);
      cont = cont + 1;

    }
  }
}

Solution

  • I solved it by changing the format for both of the spreadsheets so that they tally to each other, Thank you for the guidance.