Search code examples
javascriptgoogle-apps-scriptzillow

Finding pattern in data and structuring it


I've a sheet where I paste data from the website, when I paste, it look like this in sheet(Page 1 and Page 2):-

Here Color Code cells represent the data of one plot, like address,price,area and in address there is pincode

enter image description here

enter image description here

I am trying to make it structured like this if you take first two column block in P 1 in above image, I hope now it make some sense:-

  • I need to grab the Pincode from address

  • Address can be anywhere in columns, and below them will be there selling/renting details, like price, area etc

enter image description here

Don't know how to start, column has multiple property data which I want to structure

Here is the sheet link:- https://docs.google.com/spreadsheets/d/1M9YUR2NEc0IUvpwmzw1diMSMG9ukZw-269Rvg531WqY/edit#gid=0

Any suggestions?


Solution

  • Based on a fixed order of the data, starting at the address after that the price and after that the area

    Delete unrelated data to avoid bugs

    function findPatterns() {
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      const ssname = ss.getSheetByName('Sheet1')
    // ranges to process
      let columns = ['A24:A87', 'B24:B267', 'C24:C174']
      let res = []
      let arr = []
      columns.forEach(column => {
        let range = ssname.getRange(column).getValues().flat().filter(r => r)
        range.forEach(e => {
          let addres = e.match(/, CO \d/)
          let price = e.match(/^\$\d+(,\d+)?/gm)
          let area = e.match(/sqft|acres/)
          // added the check variable 
          let days = e.match(/Zillow/)
          if (addres != null) {
            let pincode = e.match(/, CO (\d+)/).pop()
            arr.push(e, pincode)
          }
          if (price != null) {
            arr.push(e)
          }
          // check if its the line of the days
          if(days != null){
             arr.push(e.match(/\d+/)[0])
    
             res.push(arr)
             arr = []
          }
          if (area != null) {
            arr.push(area[0])
            let num = e.match(/\d+(\.\d+)?/)
            if (num != null) {
              arr.push(num[0])
            } else {
              arr.push('')
            }
            
          }
        })
      })
      res.forEach((el, idx) => {
        range = ssname.getRange('H' + (idx + 2) + ':L' + (idx + 2))
        range.setValues([el])
      });
    }