[英]Google-Spreadsheet Scripts
I have a column of cells in a particular sheet of Google Spreadsheet document. 我在Google Spreadsheet文档的特定工作表中有一列单元格。 This column references multiple values in another sheet using the built-in JOIN command:
此列使用内置的JOIN命令引用另一个工作表中的多个值:
=JOIN(", ",Regular!B3,Regular!B9,Regular!B10,Regular!B11,Regular!B12,Regular!B13,Regular!B14)
typical output for each such cell is a list of integers that are comma-separated, f.ex: 每个此类单元格的典型输出是以逗号分隔的整数列表f.ex:
2, 5, 10, 12, 13 2,5,10,12,13
Some cells use ranges like this: 一些单元格使用如下范围:
=JOIN(", ",Regular!B3:B9)
I want to lock these cells in the formula as such: Regular!$B$3,Regular!$B:$9... 我想将这些单元格锁定在公式中,例如:Regular!$ B $ 3,Regular!$ B:$ 9 ...
Right now I want each reference to lock both column and row, but a solution that lets me pick row, column or both is a better solution. 现在,我希望每个引用都锁定列和行,但是让我选择行,列或两者兼有的解决方案是一个更好的解决方案。
1) I haven't found a way to do this without using a custom script - have I missed something? 1)我没有找到一种不使用自定义脚本的方法-我错过了什么吗?
2) My custom script solution is unfinished: 2)我的自定义脚本解决方案未完成:
function eachCellInRange(range, op) {
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
op(range.getCell(i,j), i, j);
}
}
};
function lockCell(cell, row, col) {
var formula = cell.getFormula();
if(formula) {
var startIdx = formula.indexOf('(');
if(startIdx > 0) {
//!! REGEX HERE !! //
cell.setValue(formula);
}
}
}
function lockRows() {
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
eachCellInRange(range, lockCell);
};
I need to make a regex pattern that will identify the B3, B9... parts of the formula and change them to $B$3, $B$9... but also not break in the B1:B8 case 我需要制作一个正则表达式模式,该模式将标识公式的B3,B9 ...部分并将其更改为$ B $ 3,$ B $ 9 ...,但也不要在B1:B8情况下中断
Currently all references are prefixed with SheetName! 当前,所有引用都以SheetName为前缀! (eg Regular!B9:B20), in the future some may not be, so the most general solution is preferred.
(例如,Regular!B9:B20),将来可能不会出现,因此首选最通用的解决方案。
I'm not sure whether this is what you're looking for but I would replace the little bit you currently have: 我不确定这是否是您要寻找的东西,但是我将替换您当前拥有的一点:
if(formula) {
var startIdx = formula.indexOf('(');
if(startIdx > 0) {
//!! REGEX HERE !! //
cell.setValue(formula);
}
}
by 通过
if(formula.substring(0,6) == "=JOIN(") {
formula = formula.replace(/([A-Z]+(?=[0-9]))/g, function($1) {
return "$" +$1 + "$";
});
alert(formula);
// cell.setValue(formula);
}
Which ensures that the formula is a JOIN
formula. 这样可以确保该公式是一个
JOIN
公式。
Also, I'm not that familiar with JS, but I put it in JSFiddle to see how it goes. 另外,我对JS不太熟悉,但是我将其放在JSFiddle中以了解其运行方式。
Warning: This will fail if your sheet names have alphanumeric characters (mix of letters and digits). 警告:如果工作表名称包含字母数字字符(字母和数字的混合),则此操作将失败。
Using @Jerry's useful answer, I was able to suit it to my needs: 使用@Jerry的有用答案,我能够使其适合我的需求:
function eachCellInRange(range, op) {
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
op(range.getCell(i,j), i, j);
}
}
};
var lockOn = 1, lockOff = -1, lockNop = 0,
lockChar = '$', lockEmpty = '';
function lock2char(newLock, curLock) {
if(newLock == lockNop) newLock = curLock;
return (newLock > lockNop) ? lockChar : lockEmpty;
}
function bool2lock(boolValue) {
return (boolValue) ? lockOn : lockOff;
}
function lockCell(lockCol, lockRow, cell, row, col) {
var formula = cell.getFormula();
if(formula) {
var startIdx = formula.indexOf('(');
if(startIdx > 0) {
var newFormula = formula.replace(/([A-Z|\$]+(?=[0-9]))/g, function(part) {
var prefix = lock2char(lockCol, (part.charAt(0) == lockChar));
var suffix = lock2char(lockRow, (part.charAt(part.length -1) == lockChar));
part = part.replace(/\$/g, '');
return prefix + part + suffix;
});
cell.setFormula(newFormula);
}
}
}
function lockRows() {
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
eachCellInRange(range, lockCell.bind(this, lockOff, lockOn));
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.