链接到按钮的应用脚本

我想将脚本编辑器中的现有代码链接到我的地摊图上的图像(将是一个按钮)。

我看过其他文章,有人推荐类似onClickImage {....}的代码,但这不起作用。

按钮将运行的代码如下:

// The numeric index of the column you wish to keep auto-sorted. A = 1,B = 2,// and so on.
var SORT_COLUMN_INDEX = 2;
// Whether to sort the data in ascending or descending order.
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
  } );
}

/**
 * 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);
  }
}

/**
 * Runs when the sheet is opened.
 *
 * @param {Object} event The triggering event.
 */
function onOpen(event) {
  activeSheet = Spreadsheetapp.getactiveSheet();
  autoSort(activeSheet);
}

/**
 * Runs when the add-on is installed; calls onOpen() to ensure any initializion
 * work is done immediately.
 *
 * @param {Object} event The triggering event.
 */
function onInstall(event) {
  onOpen(event);
}

到目前为止,我还没有任何工作,所以请您帮忙吗?

pibenxiai7 回答:链接到按钮的应用脚本

function sortActiveSheet() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
  rg.sort({column:1,ascending:true}); 
}

稍微复杂一点:

function sortActiveSheet() {
  var hdrs=1;
  var sortcol=1;
  var asc=true;
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getRange(hdrs+1,sh.getLastRow()-hdrs,sh.getLastColumn());
  rg.sort({column:sortcol,ascending:asc}); 
}
,

要创建带有脚本的按钮,您必须:

  1. 创建要在单击按钮时运行的功能。为此,请转到脚本编辑器,并添加以下代码(您可以根据需要对其进行修改):
    function onClickButton() {
      var sheet = SpreadsheetApp.getActiveSheet();
      autoSort(sheet);
    }
    
  2. 将任何图像插入工作表文档中。为此,请转到Insert> Image> 单元格上的图像
  3. 选择新图像,然后单击右上角的按钮,其中显示三个点。它应该如下所示:

enter image description here

  1. 单击“分配脚本”,然后在文本框中键入onClickButton
本文链接:https://www.f2er.com/3164115.html

大家都在问