How do I deselect an active cell when clicking on an image to run a script?

前端 未结 3 1926
再見小時候
再見小時候 2021-01-29 04:28

I have a spreadsheet-bound script that is invoked by clicking an image in the spreadsheet. I\'ve found that the script can be blocked if a cell that it needs to modify is active

3条回答
  •  梦谈多话
    2021-01-29 05:33

    You can use Sheet.setActive() to move the attached user's cursor to a different location in the spreadsheet. You need to remember that there is a relationship between the User Interface and scripts invoked from it, which is what allows the script to determine and change the currently active cell. This does not extend to the multiple user case, though - if User1 is editing the guarded cell when User2 clicks to run the script, the script will have no idea about it.

    function newf() {
      var safePlace = "A1"; // Some safe cell
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheets()[0];
      var activeCell = ss.getActiveCell();
      if (activeCell.getSheet() == sheet && activeCell.getA1Notation() == "R4") 
        sheet.setActiveSelection(safePlace);
      var range = sheet.getRange("R4");
      var activeCell = ss.getActiveCell();
      if (activeCell.getA1Notation() == range.getA1Notation() && activeCell.getSheet().getName() == range.getSheet().getName()) {
        // Move user from target cell
        sheet.setActiveSelection(safePlace);
      range.clear();
    }
    

提交回复
热议问题