脚本由编辑器触发时触发所有者案例

时间:2019-03-25 22:07:07

标签: javascript google-apps-script

当我的google-doc编辑器触发脚本时,它会同时触发我自己的案例。我该如何预防?

我尝试了getActiveUser和getEffectiveUser,只有不同之处才被激活,这仅触发了我的情况。当前有2个触发器,分别是我的一个触发器和另一个是由各自的gmail帐户创建的编辑器的触发器。

function timestamp(e) {
  var count = 0
  var doc = DocumentApp.getActiveDocument();
  var me = "xxx@gmail.com"
  var userName = Session.getActiveUser().getEmail();
  var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM"+" HH:mm");
  var body = doc.getBody();
  console.log();

  if (!count){
  switch (userName){
    case "xxx@gmail.com":
      if (userName == me){
      body.editAsText().replaceText(">Xxx<.*$", ">Xxx< " + date);
      count = 1;
      break;
      }
    case "zzz@gmail.com":
      body.editAsText().replaceText(">Zzz<.*$", ">Zzz< " + date);
      count = 1;
      break;       
}
}
}

对于打开文档的用户,输出应该是日期和时间的时间戳

控制台1

{
 insertId: "mh4nwqe6ptif"  

labels: {
  script.googleapis.com/deployment_id: "AKfycbwfuWqQks2U7sKVzpX26w8lL-hm0CciOGeZ4EmtOUUi"   
  script.googleapis.com/process_id: "EAEA1GOyI3tGd4a3vorh4JNBVOHgi6RgYtgFwrZD0nvmTX_Cf0-bmsPRXgqyH6DUJSet4rxj4CCOdWLDY1NjQG1qFzsVDoB9LatI74BPQ4_8R6K1Ek6cBBXr0JyBbWMXTtPbPe_Ne4mneJWnF3oSuDEtiOv3XYBHwWJ0mKQ"   
  script.googleapis.com/project_key: "MWWPE_X-NNcz9aBsDbXTBen9WCo1QLwkg"   
  script.googleapis.com/user_key: "ABkUwjbdkw+qwq/LIW606KMmYd6VVd6glH8f8PVSWy8MdK8XykhyjpFux1uTehrMsXoQJEjH8Uv8"   
 }
 logName: "projects/project-id-9053084063274654670/logs/script.googleapis.com%2Fconsole_logs"  
 receiveTimestamp: "2019-03-26T20:12:34.358323783Z"  

resource: {

labels: {
   function_name: "timestamp"    
   invocation_type: "event"    
   project_id: "project-id-9053084063274654670"    
  }
  type: "app_script_function"   
 }
 severity: "WARNING"  
 textPayload: "The script does not have permission to get the active user's identity."  
 timestamp: "2019-03-26T20:12:34.114Z"  
}

控制台2

 {
 insertId: "pszlqvei0ldj"  

labels: {
  script.googleapis.com/deployment_id: "AKfycbwfuWqQks2U7sKVzpX26w8lL-hm0CciOGeZ4EmtOUUi"   
  script.googleapis.com/process_id: "EAEA1GOyS_38lVqx6GOormX6vTH7wk4L_iK_koMbvlaUgunXHMST8CqgrmFYGcVn7suDHZf1egvGGL2XGqmmXgKqxN1IXlyBoMEt3prWsNpZnMTok5BGvUat_-KTnmv471C5-l3ATXFXQvvEEzkGJTPW7fBFLE71JBtvex1XOgd0"   
  script.googleapis.com/project_key: "MWWPE_X-NNcz9aBsDbXTBen9WCo1QLwkg"   
  script.googleapis.com/user_key: "ABkUwjbdkw+qwq/LIW606KMmYd6VVd6glH8f8PVSWy8MdK8XykhyjpFux1uTehrMsXoQJEjH8Uv8"   
 }
 logName: "projects/project-id-9053084063274654670/logs/script.googleapis.com%2Fconsole_logs"  
 receiveTimestamp: "2019-03-26T20:12:33.950683014Z"  

resource: {

labels: {
   function_name: "timestamp"    
   invocation_type: "event"    
   project_id: "project-id-9053084063274654670"    
  }
  type: "app_script_function"   
 }
 severity: "DEBUG"  
 textPayload: ""  
 timestamp: "2019-03-26T20:12:33.801Z"  
}

分隔码

function Xxx(e) {
  var doc = DocumentApp.getActiveDocument();
  var userName = Session.getEffectiveUser().getEmail()
  var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM"+" HH:mm");
  var body = doc.getBody();
  if (userName == 'Xxx@gmail.com' ) {
    body.editAsText().replaceText(">Xxx<.*$", ">Xxx< " + date);
  }
}

^^^对所有其他6个用户重复

1 个答案:

答案 0 :(得分:1)

因此,正如TheMaster所建议的那样,我认为我无法获得想要的全自动解决方案,但是我找到了解决方法

function onOpen(e) {
  DocumentApp.getUi().createAddonMenu()
      .addItem('timestamp' , 'Dontrun')
      .addToUi();
}

function Dontrun(e) {
  var doc = DocumentApp.getActiveDocument();
  var userName = Session.getActiveUser().getEmail();
  var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM"+" HH:mm");
  var body = doc.getBody();

  switch (userName){
    case "xxx@gmail.com":
      body.editAsText().replaceText(">Xxx<.*$", ">Xxx< " + date);
      break;
    case "zzz@gmail.com":
      body.editAsText().replaceText(">Zzz<.*$", ">Zzz< " + date);
      break;       
}
}