2016-08-09 31 views
1

我是一個相對的新手。我在Google電子表格中有一長串費用清單,我希望它能夠在打開工作表時根據當前月份跳到此列表中的相應位置。每個月有200行,所以我想爲它做類似:根據今天的日期打開一個Google表格到特定的地方

= MONTH(TODAY())*200 

但這並不腳本里面工作。我試圖從執行此功能的單元格拉它,但我不知道如何做到這一點。在下面的例子,我可以跳轉到九月,因爲我定義

var monthrow = 1800 

但如何做到這一點的基礎上今天的日期幾個月的休息嗎?感謝您的幫助!

function onOpen() { 

    goToSheet2b() 
    } 

    function goToSheet2b() { 

    var monthrow = 1800 
    goToSheet("Expenses", monthrow, 2); 
    } 

    function goToSheet(sheetName, row, col) { 
    var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName); 
    SpreadsheetApp.setActiveSheet(sheet); 
    var range = sheet.getRange(row, col) 
    SpreadsheetApp.setActiveRange(range); 

回答

0

如果我理解正確的你,如果每月確實有200行開始一月那麼下面應該爲你工作。我也評論過這些代碼。如果您有任何問題,請告訴我。

function onOpen() { 
    var d = new Date(new Date().getTime()); 
    var month = d.getMonth(); //.getMonth() returns values form 0 (Jan) to 11 (Dec) 
    var monthrow = month*200 + 1; //we add the 1 because if it is Jan than 0*200 = 0 + 1 = 1 or row number 1 and for the rest it will be the start of that month 
    goToSheet("Expenses", monthrow, 2); 
} 
+0

不得不將其更改爲var monthrow =(month + 1)* 200;但除此之外,它的作品。謝謝! –

+0

雖然在我的手機上打開工作表時不起作用!爲什麼會這樣? –

+0

我很高興它爲你工作一點點調整(我假設Jan從第一行開始)。我不知道爲什麼它不適用於你的手機。也許與應用程序訪問表單的方式有關?我知道該應用程序的功能有限。如果您的問題已得到解答,請考慮接受答案作爲解決方案。 – mongoose36

0

謝謝!我不得不調整操作順序。這是對我來說是完整的東西:

function onOpen() { 

    goToSheet2b() 
    } 

    function goToSheet2b() { 

    var d = new Date(new Date().getTime()); 
    var month = d.getMonth(); //.getMonth() returns values form 0 (Jan) to 11 (Dec) 
    var monthrow = (month+1)*200;//we add the 1 because if it is Jan than 0*200 = 0 + 1 = 1 or row number 1 and for the rest it will be the start of that month 
    goToSheet("Expenses", monthrow, 2); 

    } 

    function goToSheet(sheetName, row, col) { 
    var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName); 
    SpreadsheetApp.setActiveSheet(sheet); 
    var range = sheet.getRange(row, col) 
    SpreadsheetApp.setActiveRange(range);} 
相關問題