简体   繁体   English

setValue() 用于多个单元格

[英]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.我使用下面的函数来连接 Google Sheets 中单元格的值和一个超链接来在我的 ss 上创建一个HYPERLINK()函数。

    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):我通常把它们排成一排这样 (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.我尝试使用.foreach(function(r){return r[0]}) *,但由于某种原因永远无法正确使用。 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". *这是我尝试创建的函数,它返回为“null”。 I also tried range.forEach(function(r) , with no luck.我也试过range.forEach(function(r) ,但没有运气。

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:鉴于您使用getActiveCell() ,看起来您的工作流程是:

  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 .要在选择多个单元格时使用相同的工作流程,您需要使用Spreadsheet.getActiveRangeList()方法 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)选择多个范围的连续单元格(即使用 CTRL + 左键单击)
// 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 .这使得它在处理大范围时更快,并且是记录在案的最佳实践之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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