简体   繁体   中英

My "From spreadsheet - On form submit" trigger is getting disabled every time I try to run it - how do I find out why?

I have a Google form that collects reddit usernames in a Google sheet. I have a script to check whether the username doesn't exist (ie https://www.reddit.com/user/<username> gives a 404) or is banned (username looked up against a list var banned_users = [...] ). If it is non-existent or banned, I mark the row orange. Here's the script:

function validateUser() {
   var spreadsheet = SpreadsheetApp.openById(sheet_id);
   var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
   var last_row_number = sheet.getLastRow();
   var reddit_username = sheet.getRange(last_row_number, 2).getValue();
   var reddit_username_cell = sheet.getRange(last_row_number, 2);
   var reddit_username_row = sheet.getRange(last_row_number, 1, 1, 19);

   if (banned_users.indexOf(reddit_username) != -1) {
    reddit_username_cell.setNote('This user is banned.');
    reddit_username_row.setBackground('#ff9900')
  } else {
     var url = 'https://www.reddit.com/user/' + reddit_username.slice(2) // Get rid of the leading 'u/'
     var options = {
        'muteHttpExceptions': true,
        'followRedirects': true
     };
     var response = UrlFetchApp.fetch(url, options);
     var http_code = response.getResponseCode();

     if (http_code == '404') {
       reddit_username_cell.setNote('This user does not exist.');
       reddit_username_row.setBackground('#ff9900') // #ff9900 is default orange
    }
  }
}

This script works when I manually run the function. It successfully marks banned or non-existent users when I make a form submission and trigger it from the script editor. However when I add a trigger to run it on form submit, and then make a form submission, it gets disabled:

触发器禁用 未知原因

I don't understand why. There is no email explaining the reason or logs showing that it ran at all. How do I find out why this trigger is getting disabled and how do I fix it? Thank you!

When running it from a standalone script (not Sheets-bound) I get the following error:

Please select an active spreadsheet first. (line 11, file "Code")

upon executing the line:

var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);

Triggers do run scripts in a standalone fashion too, so I suggest you avoid using active sheets. Instead, you can simply replace above line of code for the following:

var sheet = spreadsheet.getSheets()[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM