A
A
Andrew20202020-07-20 20:10:45
Google Apps Script
Andrew2020, 2020-07-20 20:10:45

How to create a function in Google Script to determine if there is a link in a cell?

Are we talking about a function that will change the numeric value of one cell depending on whether a link has appeared in another cell? If a link is added in the first cell, then the numeric value in the other cell is increased by one

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Stoyanov, 2020-07-21
@stomaks

Here is the custom function code:
Here is an example of work:
Formula base for google spreadsheet

/**
 * @OnlyCurrentDoc
 *   EN: Restricts script access to the current sheet only.
 *   RU: Ограничивает доступ скрипта только к текущей таблице.
 */

/* exported hasLink */
/* globals sorosWrapper_ */

/**
 * : Checks if the range contains links.
 * 
 * : Проверяет есть ли в диапазоне ссылки.
 * 
 * : 1.0
 * 
 * : Stoyanov Maksim (stomaks)
 * : stomaks.me
 * : G-Apps-Script.COM
 * 
 * @param {[cell_address]} cell_address
 *   : Cell address.
 * 
 *   : Адрес ячейки.
 *
 * @return
 *   : true - if there are links in the range, false - if not.
 * 
 *   : true - если в диапазоне есть ссылки, false - если нет.
 * 
 * @customfunction
 */
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;
}

---
Maxim Stoyanov (stomaks), developer of Google Apps Script .
g-apps-script.com
stomaks.me

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question