在受保护范围内编写 google 应用脚本

Writing a google app script on protected range

[edit] 我终于有人帮我解锁范围了,但我仍然收到以下错误。有什么见解吗?

我正在编写 gs 以通过 API 将数据发送到 MailChimp。我什至无法测试该功能是否正常工作,因为很多 sheet 都有受保护的范围。这是一个共享的 google sheet,里面有超过 30 个用户,绑定到一个表单,所以这就是它受到保护的原因(每个人都有编辑权限)。有没有办法让我在不需要文档所有者解锁所有受保护范围的情况下侵入授权?有几个 sheet 和几个保护范围,否则这对我来说是一个简单的解锁任务。我也远程工作,所以向完全非技术文档所有者解释这个过程被证明是相当困难的..

我认为这与受保护范围有关,但也许我错了?这是我的错误:

Method ScriptApp.newTrigger invoked in the global scope.Collapse
File: Attendance Email Line: 95
This function invocation will fail when the script is run as an Add-on in AuthMode.NONE.

这里是拦截器的主要部分,我认为:

function onEdit(e) { 
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var attendance = ss.getSheetByName("ATTENDANCE");
 attendance.activate();
 var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var currentSelection = SpreadsheetApp.getActiveSheet().getActiveSelection()
 var currentRow = currentSelection.getRowIndex();

 Logger.log(currentRow);
 Logger.log(currentSelection);

 return currentRow;  
 return currentSelection;

  var email = e.currentSelection[currentRow,2,1,1];
  var lead = e.currentSelection[currentRow,1,1,1];
  var latecount = e.currentSelection[currentRow,6,1,1];
  var calloffcnt = e.currentSelection[currentRow,7,1,1];
  var wfhcnt = e.currentSelection[currentRow,8,1,1];
  var wfhoccur = e.currentSelection[currentRow,9,1,1];
  var wfhremain = e.currentSelection[currentRow,10,1,1];
  var anncalloff = e.currentSelection[currentRow,11,1,1];
  var occurtotal = e.currentSelection[currentRow,21,1,1];
  var usedpto = e.currentSelection[currentRow,22,1,1];
  var usedsick = e.currentSelection[currentRow,23,1,1];
  var ptoremain = e.currentSelection[currentRow,24,1,1];
  var sickremain = e.currentSelection[currentRow,25,1,1];
      sendToMailChimp_(email,lead,latecount,calloffcnt,wfhcnt,wfhoccur,wfhremain,anncalloff,occurtotal,usedpto,usedsick,ptoremain,sickremain);

}

/**
 * Main function. Creates onEdit trigger.
 */

function myFunction (){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var attendance = ss.getSheetByName("ATTENDANCE");
  attendance.activate();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  }

  ScriptApp.newTrigger("myFunction")
  .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
  .onEdit()
  .create();  

** 还需要注意的是,此脚本的 none 正在覆盖 sheet 中的任何内容,它应该只是只读的(因此与保护范围的原因不冲突)

mailchimp 函数供参考:

    function sendToMailChimp_(email,lead,latecount,calloffcnt,wfhcnt,wfhoccur,wfhremain,anncalloff,occurtotal,usedpto,usedsick,ptoremain,sickremain){
  var payload = {
    "apikey": API_KEY,
    "id": LIST_ID,
    "merge_fields[EMAIL]": email,
    "merge_fields[LEAD]": lead,
    "merge_fields[LATECOUNT]": latecount,
    "merge_fields[CALLOFFCNT]": calloffcnt,
    "merge_fields[WFHCNT]": wfhcnt,
    "merge_fields[WFHOCCUR]": wfhoccur,
    "merge_fields[WFHREMAIN]": wfhremain,
    "merge_fields[ANNCALLOFF]": anncalloff,
    "merge_fields[OCCURTOTAL]": occurtotal,
    "merge_fields[USEDPTO]": usedpto,
    "merge_fields[USEDSICK]": usedsick,
    "merge_fields[PTOREMAIN]": ptoremain,
    "merge_fields[SICKREMAIN]": sickremain,
    "double_optin": mc_double_optin,
    "update_existing": true
  };
  var payload = JSON.stringify;
  var options = {
    "method": "patch",
    "payload" : JSON.stringify(payload)
  };

  var response = UrlFetchApp.fetch(mc_base_url,options);
  Logger.log(response)
}

试试这个:

function onEdit() 
{ 
 var ss=SpreadsheetApp.getActive();
 var sh=ss.getActiveSheet();
 var rg=sh.getActiveRange()
 if(sh.getName()=="ATTENDANCE")
 {  
   var currentRow = rg.getRow();
   var email = sh.getRange(currentRow,2).getValue();
   var lead = sh.getRange(currentRow,1).getValue();
   var latecount = sh.getRange(currentRow,6).getValue();
   var calloffcnt = sh.getRange(currentRow,7).getValue();
   var wfhcnt = sh.getRange(currentRow,8).getValue();
   var wfhoccur = sh.getRange(currentRow,9).getValue();
   var wfhremain = sh.getRange(currentRow,10).getValue();
   var anncalloff = sh.getRange(currentRow,11).getValue();
   var occurtotal = sh.getRange(currentRow,21).getValue();
   var usedpto = sh.getRange(currentRow,22).getValue();
   var usedsick = sh.getRange(currentRow,23).getValue();
   var ptoremain = sh.getRange(currentRow,24).getValue();
   var sickremain = sh.getRange(currentRow,25).getValue();
   sendToMailChimp_(email,lead,latecount,calloffcnt,wfhcnt,wfhoccur,wfhremain,anncalloff,occurtotal,usedpto,usedsick,ptoremain,sickremain);

  } 
}