2015-10-27 68 views
0

我們有一個預測表,我們需要使用Apps腳本更新D1:N1中的單元格。Google電子表格中的月份滾動計劃(從currentDate自動更新)

我可以像現在這樣做,但它只會更新如果單元格D1中的月份爲十月。當它很快冷11月:)這將不會有幫助:)

我可以用某種方式引用數組,或者如何解決這個問題?我也可以繼續這一點,但代碼將是真的,不那麼好看,至少說。

enter image description here

function myFunction() { 

var date = new Date(); 
var mt = date.getMonth(); 
var currentD  

    if (mt === 0) { 
    currentD = "JAN (qty)"; 
    }if (mt === 1) { 
    currentD = "FEB (qty)"; 
    }if (mt === 2) { 
    currentD = "MAR (qty)"; 
    }if (mt === 3) { 
    currentD = "APR (qty)"; 
    }if (mt === 4) { 
    currentD = "MAJ (qty)"; 
    }if (mt === 5) { 
    currentD = "JUNI (qty)"; 
    }if (mt === 6) { 
    currentD = "JULY (qty)"; 
    }if (mt === 7) { 
    currentD = "AUG (qty)"; 
    }if (mt === 8) { 
    currentD = "SEP (qty)"; 
    }if (mt === 9) { 
    currentD = "OKT (qty)"; 
    }if (mt === 10){ 
    currentD = "NOV (qty)"; 
    }else if (mt === 11){ 
    currentD = "DEC (qty)"; 
    } 

Logger.log("CurrentD: " +currentD); 

    var spr = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = spr.getSheets()[0]; 

//D_MONTH  
var range = sheet.getRange("D1"); 
var cell = range.getCell(1, 1); 
var D_MONTH = cell.getValue(); 
Logger.log("D_MONTH: " + D_MONTH); 


    if(D_MONTH != currentD) { 

    cell.setValue(currentD); 
    } 


    //E_MONTH  
var range = sheet.getRange("E1"); 
var cell = range.getCell(1, 1); 
var E_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("NOV (qty)"); 
    } 

    //F_MONTH  
var range = sheet.getRange("F1"); 
var cell = range.getCell(1, 1); 
var F_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("DEC (qty)"); 
    } 

    //G_MONTH  
var range = sheet.getRange("G1"); 
var cell = range.getCell(1, 1); 
var G_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("JAN (qty)"); 
    } 

    //H_MONTH  
var range = sheet.getRange("H1"); 
var cell = range.getCell(1, 1); 
var H_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("FEB (qty)"); 
    } 


    //I_MONTH  
var range = sheet.getRange("I1"); 
var cell = range.getCell(1, 1); 
var I_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("MAR (qty)"); 
    } 

    //J_MONTH  
var range = sheet.getRange("J1"); 
var cell = range.getCell(1, 1); 
var J_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("APR (qty)"); 
    } 

    //K_MONTH  
var range = sheet.getRange("K1"); 
var cell = range.getCell(1, 1); 
var K_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("MAJ (qty)"); 
    } 

    //L_MONTH  
var range = sheet.getRange("L1"); 
var cell = range.getCell(1, 1); 
var L_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("JUNI (qty)"); 
    } 

     //M_MONTH  
var range = sheet.getRange("M1"); 
var cell = range.getCell(1, 1); 
var M_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("JULY (qty)"); 
    } 

    //N_MONTH  
var range = sheet.getRange("N1"); 
var cell = range.getCell(1, 1); 
var N_MONTH = cell.getValue(); 



    if(currentD === "OKT (qty)") { 

    cell.setValue("AUG (qty)"); 
    } 






} 

回答

1

不要重複自己 - 如果你的代碼開始看上去像一個模式有一個很好的機會,你可以重構它爲更簡潔。

這應該做你所需要的。它檢查第一列,如果是當前月份則返回。如果不是,它將開始用當前的月份名稱覆蓋,而下一個將重新覆蓋到下一年。

function setDates(){ 
    var monthNames = ["Jan", "Feb", "Mar", 
        "Apr", "May", "Jun", 
        "Jul", "Aug", "Sep", 
        "Oct", "Nov", "Dec"]; 

    var currentMonths = SpreadsheetApp.getActiveSpreadsheet().getRange("D1:N1").getValues()[0]; 
    var currentMonth = new Date().getMonth(); 

    if (currentMonths[0] == monthNames[currentMonth]){return;} 

    for (var col = 0; col < currentMonths.length; col++){ 
    SpreadsheetApp.getActiveSpreadsheet() 
        .getActiveSheet() 
        .getRange(1,4+col) 
        .setValue(monthNames[(currentMonth + col > 11 ? 
             currentMonth + col - 12 : 
             currentMonth + col)]); 
    } 
} 
+0

嗨,羅賓,正是我在找的東西,你真的得到了我正在尋找的東西,現貨!這是我想到的,但沒有你的幫助也無法實現。像現在的魅力一樣工作,只需要知道(數量)montNames :) –

相關問題