我正在尝试构建一个函数来格式化标签的某些列(主表单中的表格),标题为“#34;响应 - 不要编辑。”#34;但是,每次从菜单运行脚本时,都会收到错误消息:"找不到脚本功能:FormatCWR。"
这是代码,我已经坦诚相传......
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{name:"FormatCWR", functionName:"FormatCWR"}];
ss.addMenu("Scripts", entries);
FormatCRW()
}
function FormatCRW() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetCWR = ss.getSheetByName("Responses - DO NOT EDIT");
var data = sheetCWR.getDataRange().getValues();
var newData = new Array();
for(i in data){
sheetCWR.getRange('H2:H').setNumberFormat("mm/dd/yyyy");
sheetCWR.getRange('J2:J').setNumberFormat('$0.00');
sheetCWR.getRange('K2:K').setNumberFormat('$0.00');
sheetCWR.getRange('R2:R').setNumberFormat('$0.00');
sheetCWR.getRange('BD2:BD').setNumberFormat('$0.00');
sheetCWR.getRange('BG2:BG').setNumberFormat('$0.00');
sheetCWR.getRange('BJ2:BJ').setNumberFormat('$0.00');
sheetCWR.getRange('S2:S').setNumberFormat('[h]:[m]:[s]');
sheetCWR.getRange('T2:T').setNumberFormat('[h]:[m]:[s]');
sheetCWR.getRange('BO2:BO').setNumberFormat('[h]:[m]:[s]');
break;
}
};
答案 0 :(得分:0)
我认为该函数位于“.gs”文件中并且确实存在。所以其他地方可能出现了问题。我知道很多人以与你正在做的相似的方式制作菜单。但是,我喜欢这样做。
SpreadsheetApp.getUi().createMenu('Scripts')
.addItem('Format','FormatCRW')
.addToUi();
也许这会有所帮助......也许不会。
我可能在这里遗漏了一些东西,但这个功能对我来说毫无意义。
function FormatCRW() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetCWR = ss.getSheetByName("Responses - DO NOT EDIT");
var data = sheetCWR.getDataRange().getValues();
var newData = new Array();//Not used at all
for(i in data){//Why are you looping on every row in the data range when all of the ranges cover all of the rows?
sheetCWR.getRange('H2:H').setNumberFormat("mm/dd/yyyy");
sheetCWR.getRange('J2:J').setNumberFormat('$0.00');
sheetCWR.getRange('K2:K').setNumberFormat('$0.00');
sheetCWR.getRange('R2:R').setNumberFormat('$0.00');
sheetCWR.getRange('BD2:BD').setNumberFormat('$0.00');
sheetCWR.getRange('BG2:BG').setNumberFormat('$0.00');
sheetCWR.getRange('BJ2:BJ').setNumberFormat('$0.00');
sheetCWR.getRange('S2:S').setNumberFormat('[h]:[m]:[s]');
sheetCWR.getRange('T2:T').setNumberFormat('[h]:[m]:[s]');
sheetCWR.getRange('BO2:BO').setNumberFormat('[h]:[m]:[s]');
break;//Why have a loop if your going to break out the very first time?
}
};
这样会更有意义
function FormatCRW() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetCWR = ss.getSheetByName("Responses - DO NOT EDIT");
sheetCWR.getRange('H2:H').setNumberFormat("mm/dd/yyyy");
sheetCWR.getRange('J2:J').setNumberFormat('$0.00');
sheetCWR.getRange('K2:K').setNumberFormat('$0.00');
sheetCWR.getRange('R2:R').setNumberFormat('$0.00');
sheetCWR.getRange('BD2:BD').setNumberFormat('$0.00');
sheetCWR.getRange('BG2:BG').setNumberFormat('$0.00');
sheetCWR.getRange('BJ2:BJ').setNumberFormat('$0.00');
sheetCWR.getRange('S2:S').setNumberFormat('[h]:[m]:[s]');
sheetCWR.getRange('T2:T').setNumberFormat('[h]:[m]:[s]');
sheetCWR.getRange('BO2:BO').setNumberFormat('[h]:[m]:[s]');
}