Dnz
Dnz

Reputation: 3

How to show my array length in google script?

I customized hreflang parser for my convenience. I wanted to add a counter that will show the total quantity of the languages for each website. I'm not proficient and in this and can't give you more details. Please see below for code...

  function extractHreflang(url) {
  var regex = /<link.*?>/g;
  var hreflangReg = /hreflang="(.*?)"/gi;
  var hrefReg = /href="(.*?)"/gi;
  
  var data = UrlFetchApp.fetch(url, { 'muteHttpExceptions': true, 'followRedirects': true }).getContentText();
  var result = data.match(regex);
  var results = [];
  var count = [];
  



  if(result) {  
    var n = 0;

      if(result[n].match(hreflangReg) < result.length) {
        var hreflang = getMatches(result, hreflangReg, 1).toString();
        results.push(hreflang);

      }

    if(results.length > 0) {
      return results;  
    } else {
      return 'No hreflang tags found';
    }

  } else {
    return 'Couldn\'t read URL';
  }    
}



    // FUNCTION: Get data from RegEx capture groups
    function getMatches(string, regex, index) {
      index || (index = 1); // default to the first capturing group
      var matches = [];
      var match;
      while ((match = regex.exec(string))) {
        matches.push(match[index]);
      }
      return matches;
    }

How would I fix this so that will be reflected in column C?

enter image description here

Thanks for your time! Any help on this would be great!!

Upvotes: 0

Views: 189

Answers (1)

Tanaike
Tanaike

Reputation: 201358

I believe your goal as follows.

  • You want to put the length of the array of getMatches(result, hreflangReg, 1) to the column "C".

In this case, how about modifying your script as follows?

From:

var hreflang = getMatches(result, hreflangReg, 1).toString();
results.push(hreflang);

To:

var hreflang = getMatches(result, hreflangReg, 1);
results.push([hreflang.toString(), hreflang.length]);

Upvotes: 1

Related Questions