繁体   English   中英

如何使用Google脚本在Google电子表格中除特定列以外的所有工作表大写?

[英]How to capitalize all sheet except specific columns on Google spreadsheet using Google Script?

该代码将所有工作表单元格大写,除了某些需要的列。 但是,删除排除单元格中的公式。 我不希望脚本将我的公式擦除到这些列中。

 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(); }); } 

请注意Zig的评论。 由于几乎不需要更改,因此,请用下面的功能替换您的适当功能,然后看看是否可行。

更新的代码(要将公式保存在排除的列中,请参见注释)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM