0

我是Google腳本新手。我在A列中有一個包含日期列表的電子表格,我希望腳本循環訪問此列,並將符合特定規範的任何月份(例如,5到10之間的任何月份)放入變量中。在Google腳本中,如何獲得列中每個單元的月份?

這裏是我當前的代碼:

function CalculateInterest(values) { 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheets()[0]; 
    var date_column = sheet.getRange("A3:A"); 

    var current_cell_date = 0; 
    var current_cell_month = 0; 

    for (var x = 3; x < 100; x++) { 
    current_cell_date = date_column.getCell(x, 1); 
    current_cell_month = month(current_cell_date); 
    } 

    return current_cell_month; 
} 

現在,我停留在簡單地從細胞X 1日起得到月份數(X作爲變量)。我的電子表格顯示:ReferenceError:「月」未定義。 (第12行)。

如果我可以成功地檢索在這個循環中任何給定的細胞的月份數,然後我知道我可以比較的IF聲明該號碼。例如,我可以創建一個包含符合我的規範的月數的變量。

謝謝!

回答

2

嘗試......

function CalculateInterest(values) { 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheets()[0]; 
    var date_column = sheet.getRange(3,1,sheet.getLastRow()-2).getValues(); 

    for(var i=0;i<date_column.length;i++){ 
    var currentDate = Utilities.formatDate(new Date(date_column[i][0]), "GMT", "MM"); 
    Logger.log(currentDate); 
    } 
} 
1

這裏是在列1得到月份和十一月份非包容性的日期之間的月份,並把每月數量在那段時間中列的函數2.

function myFunction() { 
    var ss=SpreadsheetApp.getActive(); 
    var sht=ss.getSheetByName('Sheet13'); 
    var rng=sht.getDataRange(); 
    var vals=rng.getValues(); 
    for(var i=0;i<vals.length;i++) 
    { 
    if(new Date(vals[i][0]).getMonth()>4 && new Date(vals[i][0]).getMonth()<9) 
    { 
     vals[i][1]=new Date(vals[i][0]).getMonth()+1; 
    } 
    } 
    rng.setValues(vals); 
} 

這是我運行後的電子表格。

enter image description here

相關問題