I am using a script that checks column range B2:B for a specific value contained in a cell of another column (the value in this cell deliberately corresponds to the last value in the column B) and moves the cursor/pointer to the correspondent cell in column B. The final goal is basically to have automatically the cursor/pointer on the last value of the column.
The script works perfectly if run manually, but if triggered when a form is submitted, the script doesn't work. Do you have any ideas why this is happening?
Link to the form: http://goo.gl/forms/n54MIhxaSw Link to the responses file: https://goo.gl/RXNqNJ
This is the script:
function gotocell() {
Utilities.sleep(1000);
var sheet = SpreadsheetApp.getActiveSheet();
var row = SpreadsheetApp.getActiveSheet().getLastRow();
var dateRange = sheet.getRange('B2:B');
var dates = dateRange.getValues();
var cursor = sheet.getRange('D2').getValue();
// Find cursor in the dateRange
var found = false;
var row=dates.length-1;
while (row >= 0 && !found) {
var col=dates[row].length-1;
while (col>=0 && !found) {
// If the array cell matches 'cursor', stop looking.
if (!(cursor > dates[row][col]) && !(cursor < dates[row][col])) {
found = true;
break;
}
col--;
}
if (!found)
row--;
}
// Use search row & col to offset to a cell relative to dateRange start
var active = dateRange.offset(row, col,1,1);
// Move user's cursor to cursor's date, if it is found in dateRange
if (found)
sheet.setActiveRange(active);
}