2014-04-18 4 views
0

У меня есть таблица, в которой мы планируем планирование. Из столбца B до последнего возможного столбца указаны даты в строке A2. Я попытался написать функцию, которая помещает ваш курсор в эту ячейку. Но я немного застрял, и мое знание javascript ограничено.Google Spreadsheet select column current date

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 
} 

Есть ли кто-нибудь, кто может сказать мне, где я ошибся?

+0

Не могли бы вы поделиться образцом таблицы или объяснить, как выглядит лист. Я не мог понять: «Из столбца B до последней возможной колонки есть даты в строке A2» – Konstant

+0

Это выглядит так: https://dl.dropboxusercontent.com/u/26009685/ScreenShot035.jpg –

+0

у вас есть тот же (даты) в строках тоже, правильно? Я спрашиваю, потому что везде вы использовали 'rows' – Konstant

ответ

1

Я нашел его и добавил еще несколько настроек, например, не нужно вводить имя листа. Вы просто делаете лист, в котором вы используете первый в очереди.

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 за вашу помощь Konstant!

+0

удивительный. +1 для того, чтобы понять это сами – Konstant

0

Вот отредактированная функция getTodayRow, которую вы могли бы использовать. Вы можете изменить это согласно вашему требованию.

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

Я использовал (новая дата (rowContent [n]) == сегодня), но она по-прежнему относится только к первому столбцу в строке. Кроме того, это выглядит довольно хорошо, хотя :) –

+0

my bad .. use 'new Date (rowContent [n]). GetDate()'. Функция getDate извлекает часть даты из даты. – Konstant

+0

теперь он выбирает только январь: s –