Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
3
Indexable
Never
function collectPhoneNumbers() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var dataRange = sheet.getDataRange();
  var data = dataRange.getValues();
  
  var phoneNumbersByURL = {};
  
  for (var i = 0; i < data.length; i++) {
    var url = data[i][0];
    var phoneNumbers = collectPhoneNumbersFromURL(url);
    
    if (phoneNumbers.length > 0) {
      if (phoneNumbersByURL[url]) {
        phoneNumbersByURL[url] = phoneNumbersByURL[url].concat(phoneNumbers);
      } else {
        phoneNumbersByURL[url] = phoneNumbers;
      }
    }
  }
  
  for (var i = 0; i < data.length; i++) {
    var url = data[i][0];
    var phoneNumbers = phoneNumbersByURL[url] || [];
    sheet.getRange(i + 1, 2).setValue(phoneNumbers.join(', '));
  }
}

function collectPhoneNumbersFromURL(url) {
  var response;
  
  try {
    response = UrlFetchApp.fetch(url);
  } catch (e) {
    console.error('Error fetching URL:', url, e);
    return [];
  }
  
  var htmlContent = response.getContentText();
  var pattern = /(06|07)\d{8}/g;
  var phoneNumbers = htmlContent.match(pattern) || [];
  
  var uniquePhoneNumbers = [...new Set(phoneNumbers)];
  return uniquePhoneNumbers;
}