为什么这个Google Apps脚本不自动打开?

时间:2019-09-14 15:24:57

标签: google-apps-script google-sheets

我有一个Google表格,用于注册Google表单中的条目。我设置了一个脚本,以便在每次打开工作表时自动对工作表进行排序,并设置了一个触发器以在打开时运行脚本,但是没有进行排序。我对脚本和JavaScript的了解有限。以下脚本有什么问题?

function OrderByEvent() {
// The numeric index of the column you wish to keep auto-sorted. A = 1, B = 2,
// and so on.
var SORT_COLUMN_INDEX = 10;
// Whether to sort the data in ascending or descending order. false=ascending and true=descending
var ASCENDING = true;
// If you have header rows in your sheet, specify how many to exclude them from
// the sort.
var NUMBER_OF_HEADER_ROWS = 1;

// No need to edit anything below this line for general use.
// Make an improvement? Ping me on GitHub and let me know!

// Keep track of the active sheet.
var activeSheet;

/**
 * Automatically sorts on the pre-defined column.
 *
 * @param {Sheet} sheet The sheet to sort.
 */
function autoSort(sheet) {
  // Get the entire set of data for this sheet.
  var range = sheet.getDataRange();

  // Then, if there are any header rows, offset our range to remove them from
  // it; otherwise, they will end up being sorted as well.
  if (NUMBER_OF_HEADER_ROWS > 0) {
    // Setting the second parameter of offset() to 0 to prevents it from
    // shifting any columns. Note that row headers wouldn't make much
    // sense here, but this is where you would modify it if you
    // wanted support for those as well.
    range = range.offset(NUMBER_OF_HEADER_ROWS, 0);
  }

  // Perform the actual sort.
  range.sort( {
    column: SORT_COLUMN_INDEX,
    ascending: ASCENDING
  } );
}

function onOpen(event) {
  activeSheet = SpreadsheetApp.getActiveSheet();
  {autoSort(activeSheet);
  }
}

/**
 * Triggers when a sheet is edited, and calls the auto sort function if the
 * edited cell is in the column we're looking to sort.
 *
 * @param {Object} event The triggering event.
 */

//function onEdit(event) {
//  var editedCell;

  // Update the active sheet in case it changed.
//  activeSheet = SpreadsheetApp.getActiveSheet();
  // Get the cell that was just modified.
//  editedCell = activeSheet.getActiveCell();

  // Only trigger a re-sort if the user edited data in the column they're
  // sorting by; otherwise, we perform unnecessary additional sorts if
  // the targeted sort column's data didn't change.
//  if (editedCell.getColumn() == SORT_COLUMN_INDEX) {
//    autoSort(activeSheet);
//  }
//}
}

1 个答案:

答案 0 :(得分:1)

这个答案怎么样?

修改点:

  • 在您的脚本中,用作简单触发器的onOpen()函数正在放入OrderByEvent()函数。这样,在打开电子表格时,不会运行onOpen()。即使将OrderByEvent()作为可安装触发器安装,运行OrderByEvent()时,onOpen()也不会运行。
    • 这样,用于排序的脚本将无法运行。
  • 使用OnOpen事件触发器时,可以使用事件对象。
    • 例如,可以在脚本中将SpreadsheetApp.getActiveSheet()修改为event.source。当然,可以使用event.source.getActiveSheet()
    • 使用事件对象时,可以降低处理成本。 Ref

当以上几点反映到您的脚本中时,它如下所示。请认为这只是几个答案之一。

修改后的脚本:

function autoSort(sheet) {
  var SORT_COLUMN_INDEX = 10;
  var ASCENDING = true;
  var NUMBER_OF_HEADER_ROWS = 1;
  var range = sheet.getDataRange();
  if (NUMBER_OF_HEADER_ROWS > 0) {
    range = range.offset(NUMBER_OF_HEADER_ROWS, 0);
  }
  range.sort( {
    column: SORT_COLUMN_INDEX,
    ascending: ASCENDING
  } );
}

function onOpen(event) {
  autoSort(event.source);
}
  • 在您的脚本中,脚本可以使用简单的触发器工作。因此,在这种情况下,不需要安装可安装触发器。 onOpen()会自动运行并在打开电子表格时起作用。

注意:

  • 在此修改中,它假定您的排序脚本有效。

参考文献:

如果我误解了您的问题,而这不是您想要的结果,我深表歉意。