2014-04-18 5 views
0

J'ai une feuille de calcul dans laquelle nous mettons en place la planification. De la colonne B jusqu'à la dernière colonne possible, il y a des dates sur la ligne A2. J'ai essayé d'écrire une fonction qui place votre curseur sur cette cellule. Mais je suis un peu coincé et ma connaissance de javascript est limitée.Google Spreadsheet sélectionnez la colonne date actuelle

function onOpen() { 
    getTodayRow(); 
}; 

function getTodayRow(){ 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheetByName('2014a'); 
    var rowContent = sheet.getRange('B:A2').getValues(); 
    var today = new Date().getDate(); 
    var val = 1; 
    for(var n in rowContent) 
    { 
     if (new Date(rowContent[n][0]).getDate() == today) 
     { 
      val=Number(n)+1;break 
     } 
    } 
    SpreadsheetApp.getActiveSheet().getRange('A1').setValue(val); 
    // return val; 
    // the +1 above is because arrays count from 0 and rows count from 1. (Number() is to avoid having 13+1=131 which is the default behavior unfortunately) 
    sheet.setActiveCell(sheet.getRange(2, val)); //activate on right date 
} 

Y at-il quelqu'un qui peut me dire où je me suis trompé?

+0

Pourriez-vous partager un exemple de feuille de calcul ou expliquer à quoi ressemble la feuille. Je n'ai pas pu comprendre "De la colonne B jusqu'à la dernière colonne possible il y a des dates sur la ligne A2" – Konstant

+0

Il ressemble à ceci: https://dl.dropboxusercontent.com/u/26009685/ScreenShot035.jpg –

+0

vous avez le même (dates) en lignes aussi, non? Je demande parce que partout où vous avez utilisé 'rows' – Konstant

Répondre

1

Je l'ai trouvé et ajouté quelques ajustements de plus comme ne pas avoir besoin d'entrer le nom de la feuille. Vous faites juste la feuille que vous utilisez en premier.

function onOpen() { 
    getTodayRow(); 
}; 

function getTodayRow(){ 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var rowContent = sheet.getRange(2, 2, 1, sheet.getLastColumn()).getValues()[0]; // [0] as we are using just one row i.e. 
    var today = new Date(); 
    Logger.log(today); 
    var val = 1; 
    for(var n = 0; n < rowContent.length; n++) { 
    var fDate = new Date(rowContent[n]); 
    if (fDate.getDate() == today.getDate() && fDate.getMonth() == today.getMonth() && fDate.getFullYear() == today.getFullYear()) { // assuming its a number. If it's a formatted date you use if (new Date(rowContent[n] == today) 
     val = n + 2 + 13; // + 2 because of arrays and the since the columns start from B => today 
         // + 2 + 13 to get today's date in the middle of the screen 
     break; 
    } 
    } 
    Logger.log(val); 
    sheet.setActiveCell(sheet.getRange(2, val)); //activate on right date 
} 

thx pour votre aide Konstant!

+0

impressionnant. – Konstant

0

Voici la fonction éditée getTodayRow que vous pouvez utiliser. Vous pouvez modifier cela selon vos besoins.

function getTodayRow(){ 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheetByName('2014a'); 
    var rowContent = sheet.getRange(2, 2, 1, sheet.getLastColumn()).getValues()[0]; // [0] as we are using just one row i.e. 
    var today = new Date().getDate(); 
    var val = 1; 
    for(var n = 0; n < rowContent.length; n++) { 
    if (rowContent[n] == today) { // assuming its a number. If it's a formatted date you use if (new Date(rowContent[n] == today) 
     val = n + 2; // + 2 because of arrays and the since the columns start from B 
     break; 
    } 
    } 
    Logger.log(val); 
    SpreadsheetApp.getActiveSheet().getRange('A1').setValue(val); 
    // return val; 
    sheet.setActiveCell(sheet.getRange(2, val)); //activate on right date 
} 
+0

J'ai utilisé (new Date (rowContent [n]) == aujourd'hui) mais il ne va toujours qu'à la première colonne de la rangée. Excepté cela cela semble plutôt bien :) :) –

+0

mon mauvais .. utilise 'new Date (rowContent [n]). GetDate()'. La fonction getDate extrait la partie date de la date. – Konstant

+0

maintenant il sélectionne seulement janvier: s –

Questions connexes