2017-08-20 141 views
0

我正在嘗試構建一個函數,用於格式化標題爲「響應 - 不要編輯」的選項卡(主表中的一個表)的某些列。但是,每次從菜單運行腳本時,都會收到錯誤消息:「找不到腳本函數:FormatCWR」。
下面的代碼,我已經frankensteined ...Google Apps腳本:找不到腳本功能: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

我承擔的功能是在「.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]'); 
} 
+0

謝謝!這非常有幫助。 – buildbetter