(帮助)如何:根据有多少个字符使用Google脚本插入HyperLink [英] (Help) How To: Insert a HyperLink depending on how many characters have cell Using Google Script

查看:152
本文介绍了(帮助)如何:根据有多少个字符使用Google脚本插入HyperLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有列A,我想根据从第二行开始的单元格有多少个字符,在单元格中插入超链接。例如:


  • 如果单元格A2有数据(1-2345678901)= 12个字符,则插入链接www.mysite.com/ +单元格A2的值



结果应该是www.mysite.com/1-2345678901


  • 如果单元格A2具有数据(1-2345678)= 9个字符,则插入单元格A2的链接www.mysite2.com/ +的值。


结果应该是超链接www.mysite.com/1-2345678

我需要知道这是否脚本可以被扭转或者这个脚本非常好。如果有人有一个新的想法,请随时让我知道。



脚本:



  function onEdit(e)
{
var activeCell = SpreadsheetApp.getActiveSpreadsheet()。getActiveCell();
var activeCellValue = activeCell.getValue();


var row = activeCell.getRow();
var column = activeCell.getColumn();

if(row< 2)return; //如果标题行返回
var colNums = [1]; // Coulmns,其编辑被认为是
if(colNums.indexOf(column)== -1)return; //如果列以外的其他列,则返回

var length = String(activeCellValue).length;

if(length == 12)
{
activeCell.setValue('= HYPERLINK'+'(www.mysite.com ='+ activeCellValue +';' + activeCellValue +')')
}
else if(length == 9)
{
activeCell.setValue('= HYPERLINK'+'(www.mysite2.com = '+ activeCellValue +' ; '+ activeCellValue +')');


$ / code $ / pre

解决方案

通过使用交付给它的事件数据而不是诉诸服务调用,可以调整脚本以获得更好的性能。你已经通过尽早测试纾困条件做了正确的事情,但即使是那些可以重新排序以增加提前退出的机会。

  function onEdit(e)
{
var activeCell = e.range;
var activeCellValue = e.value;

if(!e.value)return; //如果值为'未定义',则不执行任何操作

var column = activeCell.getColumn();

var colNums = [1]; //列的编辑被认为是
if(colNums.indexOf(column)== -1)return; //如果列以外的其他列返回

var row = activeCell.getRow();
if(row< 2)return; //如果标题行返回

var length = String(activeCellValue).length;

if(length == 12)
{
activeCell.setValue('= HYPERLINK'+'(www.mysite.com/'+activeCellValue+';' + activeCellValue +')')
}
else if(length == 9)
{
activeCell.setValue('= HYPERLINK'+'(www.mysite2.com / '+ activeCellValue +' ; '+ activeCellValue +')');
}
}


I have the Column "A" and I want to insert a Hyperlink to the data in the cell bases on depending on how many characters have the cell starting from 2nd row. For Example:

  • If the Cell A2 have the data (1-2345678901) = 12 characteres insert the link www.mysite.com/ + value of the cell A2

Result should be www.mysite.com/1-2345678901

  • If the Cell A2 have the data (1-2345678) = 9 characteres insert the link www.mysite2.com/ + value of the cell A2.

Result should be Hyperlink www.mysite.com/1-2345678

I needs to know if this script can be tunned or this script is very Ok. If anyone have a new idea please feel free to let me know.

SCRIPT:

    function onEdit(e)
    {
      var activeCell = SpreadsheetApp.getActiveSpreadsheet().getActiveCell();
      var activeCellValue = activeCell.getValue();


      var row = activeCell.getRow();
      var column = activeCell.getColumn();

      if(row < 2)   return; //If header row then return
      var colNums  = [1]; //Coulmns, whose edit is considered
      if(colNums.indexOf(column) == -1) return; //If column other than considered then return

      var length = String(activeCellValue).length;

      if(length == 12)
      {
        activeCell.setValue('=HYPERLINK' + '("www.mysite.com='+activeCellValue+'";"'+activeCellValue+'")')
      }
      else if(length == 9)
      {
        activeCell.setValue('=HYPERLINK' + '("www.mysite2.com='+activeCellValue+'";"'+activeCellValue+'")');
      }
    }

解决方案

Your script can be tweaked for better performance, by making use of the event data delivered to it instead of resorting to Service calls. You have done the right thing by testing for bail-out conditions early, but even those can be reordered to increase the chance of exiting early.

function onEdit(e)
{
  var activeCell = e.range;
  var activeCellValue = e.value;

  if (!e.value) return;    // do nothing if value is 'undefined'

  var column = activeCell.getColumn();

  var colNums  = [1]; //Columns, whose edit is considered
  if(colNums.indexOf(column) == -1) return; //If column other than considered then return

  var row = activeCell.getRow();
  if(row < 2)   return; //If header row then return

  var length = String(activeCellValue).length;

  if(length == 12)
  {
    activeCell.setValue('=HYPERLINK' + '("www.mysite.com/'+activeCellValue+'";"'+activeCellValue+'")')
  }
  else if(length == 9)
  {
    activeCell.setValue('=HYPERLINK' + '("www.mysite2.com/'+activeCellValue+'";"'+activeCellValue+'")');
  }
}

这篇关于(帮助)如何:根据有多少个字符使用Google脚本插入HyperLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆