如何强制脚本在Google表格的每个用户打开时提示用户授权?

时间:2019-05-08 19:57:39

标签: google-apps-script deployment google-sheets scripting authorization

我使用Google表格/脚本为学生中心创建了一个图书馆结帐系统。

它使用许多onEdit函数调用。代码不是很优化。

我遇到的两个问题是: 1)我不能强迫新用户给予授权/或看到授予授权提示。 2)在部署过程中,所有者(我自己)的授权被破坏(如下所述)。

我尝试将其部署为附件,一旦发现它不能解决我的问题(1),便撤销了该操作。

从那时起,这些功能对我来说就中断了。我试图删除当前触发器,然后重新实现它们。

在此过程中,我有多个版本,并为最新版本(我正在编辑)创建了触发事件。

如果我强制运行main函数,则会提示您进行授权。我授予它,该脚本适用于触发的最后一个onEdit事件。

然后它停止工作。

如果我再次按下“播放/运行”按钮,它将再次询问用户身份。

我不知道从哪里开始调试此问题(2),这使我离原来的问题(1)更远了。

任何见解都是有帮助的。谢谢。

在appscript.json文件中调整范围身份验证。 撤消应用程序附加部署。

/* 
TODO:
  Update the schedule remiders for renewal option DONE
  Highlighting DONE
  Popup for check in. DONE
* Fix user auth
  Popup confirm for check out. DONE
* Sheet sorting - https://stackoverflow.com/questions/35563652/automatic-sorting-on-sheets
* https://www.extendoffice.com/documents/excel/4707-google-sheets-count-number-of-occurrence.html
* historical log -> # occurances top 5-10 books
  email reminder inscript DONE
* initial reminder & 1 week out & 2 days out & when renewed
  hide student data columns w checkbox DONE
  Unhide! DONE
  public facing DONE
  dynamic fields DONE
  google calendar add CANT BE DONE
   SEE ABOVE google calendar notification for 1 week prior and 2 days prior. https://developers.google.com/apps-script/reference/calendar/calendar-event
*/

function myFunction() {  
//  authorize();
  var changelogSheetName = "Checked Out Items History.";
  var mains = "QRC Check Out";
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getActiveCell();
  var sheetName = sheet.getName();

  var datenow = new Date();
  var changelogSheet = ss.getSheetByName(changelogSheetName);
  var copyfrom = ss.getSheetByName(mains);
  var range = copyfrom.getRange("A3:N500");


  if (cell.getA1Notation() == 'J2' )
  {
    range.sort(6);
  }
  if (cell.getA1Notation() == 'K2' )
  {
    range.sort(7);
  }




  if (cell.getA1Notation() == 'A1')
  {
    if (cell.getValue() == 1){
      copyfrom.hideColumns(4,5);
      cell.setNote("Check to hide student data. Un-check to show.");
    }
    else{
      copyfrom.showColumns(4,5);
    }
  }

  if (cell.getColumn() == 1 & cell.getRow() > 2)
  {
    var ui = SpreadsheetApp.getUi();                                                                                  // Used for both if conditions.
    if(cell.getValue() == 1){

      name = ui.prompt('Enter the Students name:',ui.ButtonSet.OK);
      copyfrom.getRange(cell.getRow(), 5,1,1).setValue(name.getResponseText());

      emerzadd = ui.prompt('Enter the Students PCC E-Mail address:',ui.ButtonSet.OK);
      copyfrom.getRange(cell.getRow(), 6,1,1).setValue(emerzadd.getResponseText());

 //     var regEX = new RegExp("(pcc.edu)");
 //     var postmailone = regEX.test(emerzadd);

      gammynum = ui.prompt('Enter the Students G-Number:',ui.ButtonSet.OK);
      copyfrom.getRange(cell.getRow(), 7,1,1).setValue(gammynum.getResponseText());

      var outdate = datenow;
      copyfrom.getRange(cell.getRow(), 4,1,1).setValue(outdate);                                                      //sets current date

      var data1 = copyfrom.getRange(cell.getRow(),1,1,12).getValues();                                                // pickup newly entered data for history log
      data1[0].unshift(datenow);                                                                                      // add timedate to array to be written   
      changelogSheet.appendRow(data1[0] );                                                                            // write the row to the history log

      sendinfo(data1); // send this info to make an initial nag reminder

     // var event = CalendarApp.getDefaultCalendar().createEvent('QRC Materials Due ' + data1[11], new Date(data1[4]),new Date(data1[4] + '1:00')); // add calendar entry
      }

      else if (cell.getValue() == 0)                                                                                  // if check box gets unchecked, delete student data from working library
      {
        var erase = ui.alert('Are you sure you want to check the book back in?',ui.ButtonSet.YES_NO)
        if (erase == ui.Button.YES){
            copyfrom.getRange(cell.getRow(),4,1,5).clearContent().clearNote();
        } else {
             copyfrom.getRange(cell.getRow(), 1,1,1).setValue('TRUE');
        }      
      }                 
   }
}

function sendinfo(reminderdata){
  var reminderdata = reminderdata;
  var Sheet8a = "Sheet8";
  var details = "Details";
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var Sheet8 = ss.getSheetByName(Sheet8a);
  var deetsheet = ss.getSheetByName(details);
  confi = deetsheet.getRange(2,3,12,1).getValues();

  var qrclogoURL = confi[8];
  var qrclogoBLOB = UrlFetchApp
                       .fetch(qrclogoURL)
                       .getBlob()
                       .setName("qrclogoBLOB");

  var timenow = reminderdata[0];
  var duedate = reminderdata[2];
  var sdtname = reminderdata[5];
  var sdemail = reminderdata[6];
  var gnumber = reminderdata[7];
  var isrenew = reminderdata[8];
  var medianm = reminderdata[10];

//  nothing = ui.prompt('0' + timenow + reminderdata[0],ui.ButtonSet.OK);

 // var author1 = [ reminderdata[12], reminderdata[11] ];
//  var authors = author1.join(' ');

// Sheet8.appendRow(reminderdata);
//    for (var t = 0; t < reminderdata.length; t++){
//    Sheet8.appendRow(reminderdata[t]);
//      Sheet8.appendRow();
//  }
  subjectline = "Thanks for checking out " + medianm + " from the SYQRC. Due: " + duedate;
  MailApp.sendEmail({
        to: sdemail, 
    subject: subjectline,
    htmlBody: "wib wib",
    inlineImages:
    {
      qrcLOGO: qrclogoBLOB
    } 
  });
}

function scheduledreminder(){

  var checkedout = "Currently Checked Out Materials";
  var details = "Details";
  var Sheet8a = "Sheet8";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getActiveCell();
  var sheetName = sheet.getName();

  var checkme = ss.getSheetByName(checkedout);
  var Sheet8 = ss.getSheetByName(Sheet8a);
  var deetsheet = ss.getSheetByName(details);

  confi = deetsheet.getRange(2,3,12,1).getValues();
//  for (var t = 0; t < confi.length; t++){
//    Sheet8.appendRow(confi[t]);
//  }

  var qrcroom = confi[0];
  var building = confi[1];  
  var rf = confi[5];
  var phonenum = confi[6];
  var email = confi[7];
  var qrclogoURL = confi[8];
  var pubaxs = confi[9];

//  var qrclogoURL = "https://www.pcc.edu/queer/wp-content/uploads/sites/25/2018/06/QRC-wide-500x500.png";
  var qrclogoBLOB = UrlFetchApp
                         .fetch(qrclogoURL)
                         .getBlob()
                         .setName("qrclogoBLOB");


  data5 = checkme.getRange(3,1,checkme.getRange("B1:B500").getLastRow()-2,10).getValues();

  for (var xx = 0; xx < data5.length; xx++){
    var datdata = data5[xx];

    var daysod = datdata[1];
    var duedate = datdata[2];
    var checkdate = datdata[3];
    var namey = datdata[4];
    var emil = datdata[5];
    var booknem = datdata[9];
    var frequency = new Date();
    frequency -= duedate;
  //  Sheet8.appendRow(datdata); //Copy checked out items to page sheet 8 for debugging reasons.

    subjectline = 'Materials due: ' + booknem;
    if (daysod >= 13.5 && (daysod % 7 > 0 && daysod % 7 < 1)) {
      MailApp.sendEmail({
        to: emil, 
        subject: subjectline,
        htmlBody: "<h1><strong>Library Materials Overdue</strong></h1>" +
        "<p><strong>Hi " + namey + ",<p>" + 
        "We hope you had time to enjoy the media: " + booknem + " which you checked out on " + 
        Utilities.formatDate(checkdate, 'America/ Portland', 'EEE, MMM d, \'\'yy') + 
        ". It is now "+ Math.round(daysod) +" days overdue, and was due back on " + Utilities.formatDate(duedate, 'America/ Portland', 'EEE, MMM d, \'\'yy') + 
        ". If you have already returned the media, disregard this message. </strong></p> " +
        "<h4><strong>If you have not yet brought it back to the Sylvania QRC, located in <span style=\"color: #ff6600;\">room " +
        qrcroom + " inside the " + building + " building</span>, please do so at your earliest convenience. You may check " +
        "the material in with any advocate that is currently on shift.</strong></h4>" +
        " <p><strong>Thanks for helping us keep our library accessible to everyone. Have a great day and we hope you continue " +
        "to enjoy our library.</strong></p> " +
        "<p><strong>Phone: " + phonenum + "</strong></p> "+
        "<p><strong>Email: " + email + "&nbsp;</strong></p>" +
        "<p><strong>Open Monday-Thursday during Fall, Winter, and Spring Terms.</strong></p>" +
        "<p><strong>Mon: 10am-5pm&nbsp;</strong><br /><strong>Tues:&nbsp;10am-5pm</strong><br /><strong>Wed:&nbsp;10am-2pm</strong><br /><strong>Thurs:&nbsp;10am-6pm</strong><br /><strong>Fri:&nbsp;Closed</strong></p>" +
        "<p><strong>Summer Term:&nbsp;available by appointment</strong></p>" +
        "<p><strong>The QRC is closed during finals week and breaks between terms.</strong></p>" +
        "<p>&nbsp;</p> <p><p><strong>Sincerely,</strong></p> <p><strong>" +
        "The Librarians at ...</strong></p> <p><p><img src='cid:qrcLOGO'>",
        inlineImages:
          {
           qrcLOGO: qrclogoBLOB
          }
      });            
    }
  }
}

/* 
RESOURCES USED (no particular order):
1 https://html-online.com/editor/
https://support.google.com/a/answer/1371415?hl=en
https://developers.google.com/apps-script/reference/calendar/calendar-event
https://developers.google.com/apps-script/concepts/scopes
https://developers.google.com/apps-script/guides/services/authorization
https://support.google.com/docs/forum/AAAABuH1jm0Ujpf-tJQrCs/?hl=en&msgid=-6uY3F5kR9UJ&gpf=d/msg/docs/Ujpf-tJQrCs/-6uY3F5kR9UJ
https://stackoverflow.com/questions/19004774/popup-authorization-code-documentapp-google-apps-script
https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_programmatically
https://developers.google.com/sheets/api/guides/concepts
https://stackoverflow.com/questions/18840885/google-script-error/19853912
https://developers.google.com/apps-script/reference/script/script-app#newTrigger(String)
https://stackoverflow.com/questions/36178369/how-to-include-inline-images-in-email-using-mailapp
https://stackoverflow.com/questions/10699726/escaping-characters-in-javascript-single-and-double-quotes
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
http://patorjk.com/software/taag/#p=display&f=Doh&t=yeet%20the%20patriarchy
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
https://developers.google.com/apps-script/articles/sending_emails
https://developers.google.com/apps-script/concepts/scopes
https://stackoverflow.com/questions/46213101/can-we-hide-column-in-sheet-using-google-apps-script
https://developers.google.com/apps-script/guides/dialogs
https://developers.google.com/apps-script/guides/triggers/installable
https://developers.google.com/apps-script/reference/base/browser
https://developers.google.com/apps-script/reference/mail/mail-app
https://developers.google.com/apps-script/reference/script/clock-trigger-builder
https://developers.google.com/apps-script/reference/spreadsheet/range#clearNote()
https://developers.google.com/apps-script/reference/spreadsheet/sheet#appendRow(Object)
https://developers.google.com/apps-script/reference/spreadsheet/sheet#getDataRange()
https://stackoverflow.com/questions/11334296/google-sheet-script-set-cell-value
https://stackoverflow.com/questions/11947590/sheet-getrange1-1-1-12-what-does-the-numbers-in-bracket-specify
https://stackoverflow.com/questions/41383415/copy-and-past-multiple-rows-ljava-lang-object-response
https://stackoverflow.com/questions/45155847/google-script-project-trigger-not-running/45157034#45157034
https://stackoverflow.com/questions/51068726/set-value-of-checkbox
https://stackoverflow.com/questions/9268570/i-need-a-button-to-clear-cells-in-a-google-spreadsheet
https://support.google.com/docs/answer/3094140?hl=en
https://stackoverflow.com/questions/51131119/passing-arguments-in-google-apps-script
https://support.google.com/docs/forum/AAAABuH1jm08lc5kRzvdY8/?hl=en&msgid=DQAegY4rAwAJ&gpf=d/msg/docs/8lc5kRzvdY8/DQAegY4rAwAJ
https://webapps.stackexchange.com/questions/89063/how-to-include-multiple-if-statements-in-one-cell-in-google-sheets/89068
https://www.benlcollins.com/spreadsheets/vlookup-multiple-criteria/
https://www.extendoffice.com/documents/excel/4742-google-sheets-vlookup-multiple-values.html
https://www.portent.com/blog/analytics/google-app-script-1.htm
https://www.w3schools.com/js/js_arrays.asp
https://www.w3schools.com/jsref/jsref_unshift.asp
https://stackoverflow.com/questions/21679267/length-of-two-dimensional-array-in-javascript-google-apps-script
https://developers.google.com/apps-script/guides/support/best-practices
https://stackoverflow.com/questions/15664696/minus-equals-in-javascript-what-does-it-mean
https://www.wikihow.tech/Pull-Data-from-Another-Sheet-on-Google-Sheets-on-PC-or-Mac
https://support.google.com/docs/forum/AAAABuH1jm0lUGZ108vh-E/?hl=en&gpf=%23!topic%2Fdocs%2FlUGZ108vh-E
https://www.benlcollins.com/spreadsheets/conditional-formatting-entire-row/
https://www.w3schools.com/js/js_arithmetic.asp
*/

(1)在新用户打开工作表时,提示授权。
(2)所有者的常规功能,无需重复授权。

0 个答案:

没有答案