简体   繁体   English

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

[英]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.

相关问题 如何使用应用脚本从 Gmail CSV 附件中提取特定列到 Google 电子表格? - How to pull specific columns from Gmail CSV attachment to Google Spreadsheet using app script? 如何使用 Google Apps Script 上的电子表格一次更新一行的所有列? - How to update all columns of a row at once using spreadsheet on Google Apps Script? 如何在不使用电子表格 ID 的情况下使用 Google 脚本从另一个 Google 表格导入数据 - How to import data from another Google Sheet using Google Script without using Spreadsheet ID 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? Google电子表格脚本-迭代活动选择的行,但所有列 - Google spreadsheet Script - iterate on rows of active selection, but all columns 编写Google表格脚本,以仅在电子表格的一张表格中为每个新提交的表格提供具体的编号。 - Script google sheet to give a number specific on each new form submitted, only for one sheet of the spreadsheet. 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 如何使用脚本隐藏 Google 电子表格中的侧边栏? - How to hide the Sidebar in a Google Spreadsheet using a script? 使用谷歌脚本填充谷歌表格中的特定列 - Populate specific column in google sheet using google script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM