我目前正在为此自己编写一个函数,在google文档中找不到现有函数。
但是我想知道,这是如此标准,应该有一种默认的方式。
我只是想检查A1表示法是否在第二个A1表示法的范围内。
示例伪代码:
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
是否有默认功能?
答案 0 :(得分:1)
此代码可能存在一些问题,但也许会给您一个想法。
function isWithinA1Range(a1Notation,a1_Two) {
var columnRangeOneStart,colmRngOneEnd,columnRange2Start,colmRng2End,columnIsInside,
rng,rng2,sh,ss;
ss = SpreadsheetApp.getActiveSpreadsheet();
sh = ss.getActiveSheet();
rng = sh.getRange(a1Notation);
rng2 = sh.getRange(a1_Two);
columnRangeOneStart = rng.getColumn();
colmRngOneEnd = rng.getLastColumn();
columnRange2Start = rng2.getColumn();
colmRng2End = rng2.getLastColumn();
columnIsInside = columnRangeOneStart >= columnRange2Start && columnRangeOneStart <= colmRng2End;
rowRangeOneStart = rng.getRow();
rowRngOneEnd = rng.getLastRow();
rowRange2Start = rng2.getRow();
rowRng2End = rng2.getLastRow();
rowIsInside = rowRangeOneStart >= rowRange2Start && rowRangeOneStart <= rowRng2End;
if (columnIsInside && rowIsInside) {
return true;
}
}
答案 1 :(得分:1)
我们可以使用正则表达式来分隔字符串并分别进行测试。尝试以下示例:
/**
* @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个字母列。