A
A
Andrew20202020-08-16 15:38:12
JavaScript
Andrew2020, 2020-08-16 15:38:12

How to make custom function for google spreadsheet?

It is necessary to make the following function:

Increase by one the value of a certain cell, in which the function will be placed, if a cell located in a specific range of cells has a reference value.

The bottom line: this is a work plan in a Google spreadsheet, a media plan in which topics of publications are packed, and when a publication is released, a link is attached to the entry, as a result of which the value of the cell in which the number of published publications is fixed should increase by one. I built something here, thanks to Maxim Stoyanov, who gave a working hasLink () function. But the knowledge is clearly lacking.

function makeTotal(sells){
  const sheet = SpreadsheetApp.getActiveSpreadsheet();
 
  var range = sheet.getRange(sells);
  var values = range.getValues();
  var z = 0;
  
    for (var i=0;i < range.length;i++){
      if(hasLink(values)){z++;}
      
      }
  return z;
    }


  

      function hasLink ( cell_address ) {
  const sheet = SpreadsheetApp.getActiveSheet();
  
  const formula = sheet.getActiveRange().getFormula();
  const args = formula.match(/hasLink\(([^\)]*?)\)/i)[1].trim();
  range = sheet.getRange(args);
  
  const values = range.getRichTextValues();
  
  const result = [];
  
  for ( let cells of values ) {
    for ( let cell of cells ) {
      const link_url = cell.getLinkUrl();
      if (link_url) result.push(link_url);
    }
  }
  
  return result.length ? true : false;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2020-08-16
@ProgrammerForever

Isn't it easier to count links this way? (counts in column A):

=СЧЁТЗ(ЕСЛИОШИБКА(filter(A:A;REGEXMATCH(A:A;"(https?:\/\/)?([\w\.]+)\.([a-z]{2,6}\.?)(\/[\w\.]*)*\/?"))))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question