简体   繁体   中英

setValue() for multiple cells

I use the function below to concat the value of the cell in Google Sheets and a hyperlink to crate a HYPERLINK() function on my ss.

    function setCustomLink(){
      var ss = SpreadsheetApp.getActive().getActiveSheet();
      var cell = ss.getActiveCell();
      var cellValue = cell.getValue();
      cell.setValue('=HYPERLINK("https://projudi.tjpr.jus.br/projudi/processo/buscaProcesso.do?actionType=pesquisaSimples&-H&Host:&projudi.tjpr.jus.br&-H&User-Agent:&Mozilla/5.0&(Windows&NT&6.3;&WOW64;&rv:49.0)&Gecko/20100101&Firefox/49.0&-H&Accept:&text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&-H&Accept-Language:&pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3&--compressed&-H&Referer:&https://projudi.tjpr.jus.br/projudi/processo/buscaProcesso.do?actionType=iniciarSimples&-H&Cookie:&projudiContCookie=0;&JSESSIONID=053165f8dd5f8532c326f3eb06d7;&projudi-route=4;&dtLatC=54;&dtPC=-;&dtCookie=49542FA50EF89B032E8685F08394F120|UHJvanVkaSstK0V4dGVybm98MQ&-H&Connection:&keep-alive&-H&Upgrade-Insecure-Requests:&1&--data&page=1&flagNumeroUnico=true&flagNumeroFisicoAntigo=false&numeroProcesso='
      + cellValue + '";"' + cellValue+'")');
    }

It works just fine. The only problem is that I have to execute the function one by one, and it takes a lot of time. I usualy have them on a row like this (A2:A):

例子

Is there any way I can select the entire row and run the function once? I tried using .foreach(function(r){return r[0]}) *, but for some reason never could get it right. Thanks!

(FYI: the content is in portuguese and the function allows me to see the lawsuit status by linking its number with the search tool hyperlink).

*Here is the function I tried to make, it comes back as "null". I also tried range.forEach(function(r) , with no luck.

function myFunction() {
  var ss = SpreadsheetApp.getActive().getActiveSheet();
  var range = ss.getActiveRange().getValues();
  var value = range.forEach(function(r){ 
    '=HYPERLINK("https://projudi.tjpr.jus.br/projudi/processo/buscaProcesso.do?actionType=pesquisaSimples&-H&Host:&projudi.tjpr.jus.br&-H&User-Agent:&Mozilla/5.0&(Windows&NT&6.3;&WOW64;&rv:49.0)&Gecko/20100101&Firefox/49.0&-H&Accept:&text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&-H&Accept-Language:&pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3&--compressed&-H&Referer:&https://projudi.tjpr.jus.br/projudi/processo/buscaProcesso.do?actionType=iniciarSimples&-H&Cookie:&projudiContCookie=0;&JSESSIONID=053165f8dd5f8532c326f3eb06d7;&projudi-route=4;&dtLatC=54;&dtPC=-;&dtCookie=49542FA50EF89B032E8685F08394F120|UHJvanVkaSstK0V4dGVybm98MQ&-H&Connection:&keep-alive&-H&Upgrade-Insecure-Requests:&1&--data&page=1&flagNumeroUnico=true&flagNumeroFisicoAntigo=false&numeroProcesso=' + r[0] + '";"' + r[0]+'")'
  });
  range.setValue(value);
}

Given your use of getActiveCell() , it looks like your workflow is:

  1. select a cell
  2. execute the function to update that single cell

To use the same workflow when selecting multiple cells, you want to use the Spreadsheet.getActiveRangeList() method . This will allow you to interact with a list of all the currently selected ranges. This will work with all of the below cases:

  • selecting a single cell
  • selecting a range of contiguous cells
  • selecting multiple ranges of contiguous cells (ie using CTRL + left click)
// Use a global constant to ease editing the value should the URL need to be changed.
var URL_PREFIX = 'http://url/?param1=value1&param2=';

function setCustomLinks() {
  var activeSheet = SpreadsheetApp.getActiveSheet();
  var ranges = activeSheet.getActiveRangeList().getRanges();

  // Loop through the ranges.
  for (var i = 0; i < ranges.length; i++) {

    // Get all cell values of the range into a 2D array.
    var values = ranges[i].getValues();

    // Loop through the 2D array of values.
    for (var j = 0; j < values.length; j++) {
      for (var k = 0; k < values[j].length; k++) {

        // Replace each value with a hyperlink formula.
        values[j][k] = createCustomLink(values[j][k]);
      }
    }

    // Set the cell values using the new values.
    ranges[i].setValues(values);
  }
}

function createCustomLink(value) {
  var url = URL_PREFIX + value;
  return '=HYPERLINK("' + url + '", "' + value + '")';
}

Another benefit with this approach is that it's using batch operations to get/set each range of values in single method call. This makes it much faster when handling a large range and is one of the documented best practices .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM