Code Trigger does not work in App Google Sheets [duplicate]

旧巷老猫 提交于 2019-12-25 17:17:42

问题


I have a code that lets people jump to the current date row, when opening my sheet.

I solved the problem on desktop in the following way: Previous solution

Now this should also work in the same way on the iOS and Android App Google Sheets. However, nothing happens if I open the sheet in the App. Does someone know how to trigger jumping to the current date row if the sheet is opened in the App?

Thank you very much!


回答1:


Both simple and installable triggers are subject to restrictions and cannot be fired by viewers. I therefore suggest you the following workaround:

On every open event, hide all rows apart from the one of interest.

If you create a WebApp, you can use it to run the row hiding function even if the user opening the link does not have edit permissions.

Sample:

  • Code.gs part of the Web App
function doGet() {
    return HtmlService.createHtmlOutputFromFile("index.html")
        .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function onOpen() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getActiveSheet();
 var lastRow=sheet.getLastRow();
 sheet.showRows(1, lastRow);
 var range = sheet.getRange("B:B");
 var values = range.getValues();  
 var day = 24*3600*1000;  
 var today = parseInt((new Date().setHours(0,0,0,0))/day);  
 var ssdate; 
 for (var i=0; i<values.length; i++) {
   try {
     ssdate = values[i][0].getTime()/day;
   }
   catch(e) {
   }
   if (ssdate && Math.floor(ssdate) == today) {
               Logger.log(i);
     sheet.hideRows(i+2,lastRow-i-1);
     sheet.hideRows(1,i);
     break;
   }    
 }
}
  • index.html part of the Web App
<!DOCTYPE html>
<html>

<head>
    <base target="_top">
    <script>
        function redirect() {
            window.open("PASTEHERETHEURLOFTHESPREADSHEET","_blank");
            google.script.run.onOpen();
        }
    </script>
</head>
<body onload="redirect()">
</body>
</html>
  • Deploy the WebApp as me and give access as required:

  • Provide the users the URL of the WebApp instead of the URL to the spreadsheet.

Note: It is not possible to setActiveRange within the WebApp, since this is a user-specific view mode, rather than a general edit that you as an editor can enforce for the viewer.



来源:https://stackoverflow.com/questions/58198106/code-trigger-does-not-work-in-app-google-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!