Google脚本中是否存在现有方法来检查A1表示法是否在第二个A1表示法范围内 [英] Is there an existing way in google script to check if an A1 notation is in range of a second A1 notation

查看:49
本文介绍了Google脚本中是否存在现有方法来检查A1表示法是否在第二个A1表示法范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为此自己编写一个函数,在google文档中找不到现有函数.

I am currently writing a function for this myself, could not find an existing function in the google docs.

但是我想知道,这是如此标准,应该有一种默认的方式.

But I was wondering, this is so standard, there should be a default way to do this.

我只是想检查A1表示法是否在第二个A1表示法的范围内.

I simply want to check if an A1 notation is in range of a second A1 notation.

示例伪代码:

var range = "A33";
isWithinA1Range(range, "A3:A"); //returns true
isWithinA1Range(range, "A:A"); //returns true
isWithinA1Range(range, "A33"); //returns true
isWithinA1Range(range, "A30:B35"); //returns true
isWithinA1Range(range, "A1:A20"); //returns false
isWithinA1Range(range, "B:B"); //returns false

是否有默认功能?

推荐答案

我们可以使用正则表达式来分隔字符串并分别进行测试.尝试以下示例:

We can use regex to separate strings and test them individually. Try this sample:

/**
 * @return {boolean} Returns TRUE If the given a1Notation is in the rangeToCheck
 * @param {"A1"} a1Notation The range in A1Notation
 * @param {"A1:B1"} rangeToCheck The range in which to check the A1 Notation
 */
function withinRange(a1Notation, rangeToCheck) {
  var input = Array.prototype.map.call(arguments, function(e) {
    return e.toUpperCase();
  });
  var rangeArgs = /^([A-Z]+)?(\d+)?:([A-Z]+)?(\d+)?$/.exec(input[1]);
  var a1NotationArgs = /^([A-Z]+)(\d+)$/.exec(input[0]).map(function(e, i) {
    return i == 1 ? ('  ' + e).substr(-2) : e * 1;
  });
   /* If range arguments are missing(like missing end column in "A1:1"), add arbitrary arguments(like "A1:ZZ1")*/
  rangeArgs = rangeArgs.map(function(e, i) {
    return e === undefined ?
      i % 2 === 0 ?
        i > 2 ?
          Infinity : -Infinity
        : i > 2 ?
          'ZZ' : ' A'
      : i % 2 === 0 ?
        e * 1 : ('  ' + e).substr(-2);
  });
  console.log(rangeArgs, a1NotationArgs);
  return (a1NotationArgs[1] >= rangeArgs[1] &&
    a1NotationArgs[1] <= rangeArgs[3] &&
    a1NotationArgs[2] >= rangeArgs[2] &&
    a1NotationArgs[2] <= rangeArgs[4]);
}

以上最多支持2个字母列.

The above supports up to 2 letter columns.

这篇关于Google脚本中是否存在现有方法来检查A1表示法是否在第二个A1表示法范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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