[英]How to capitalize all sheet except specific columns on Google spreadsheet using Google Script?
This code capitalize all the sheet cells, except some columns as required. 该代码将所有工作表单元格大写,除了某些需要的列。 Nevertheless, formulas into the excluded cells are deleted. 但是,删除排除单元格中的公式。 I do not want the script erase my formulas into these columns. 我不希望脚本将我的公式擦除到这些列中。
function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu('Eta Buddy') .addItem('Capitalize', 'proper') .addToUi(); } function proper() { var s = SpreadsheetApp.getActiveSheet(), excludedCols = [2, 4, 6, 8, 10]; s.getDataRange() .setValues( s.getDataRange() .getValues() .map(function (r) { return r.map(function (el, i) { return !el ? null : (typeof el !== 'string' && el) || excludedCols.indexOf(i + 1) > -1 ? el : toTitleCase(el); }) }) ) } function toTitleCase(str) { return str.replace(/\\w\\S*/g, function (txt) { return txt.charAt(0) .toUpperCase() + txt.substr(1) .toLowerCase(); }); }
Please take note of Zig's comment. 请注意Zig的评论。 Since very little needs to be changed, replace your proper function with the one below and see if that works.. 由于几乎不需要更改,因此,请用下面的功能替换您的适当功能,然后看看是否可行。
UPDATED CODE (to save the formulas in the excluded columns, see comment) 更新的代码(要将公式保存在排除的列中,请参见注释)
function proper() {
var s = SpreadsheetApp.getActiveSheet(),
excludedCols = [2, 4, 6, 8, 10];
formulas = s.getDataRange()
.getFormulas()
s.getDataRange()
.setValues(
s.getDataRange()
.getValues()
.map(function (r, j) {
return r.map(function (el, i) {
return !el ? null : formulas[j][i] ? formulas[j][i] :
(typeof el !== 'string' && el) || excludedCols.indexOf(i + 1) > -1 ? el : toTitleCase(el);
})
})
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.