【问题标题】:Google Script returns null after 2nd call to Gmail谷歌脚本在第二次调用 Gmail 后返回 null
【发布时间】:2015-03-05 17:28:49
【问题描述】:

我正在尝试创建一个脚本,用于在用户的 gmail 中搜索存在“soundcloud.com”且更新时间为:1 天的电子邮件。然后我试图将这些 soundcloud url 拉入带有 appendRow 的电子表格中。我使用正则表达式来获取 url 和 'soundcloud.com' 之后的任何内容,直到空格。我还使用一个函数来解析 html。

在运行脚本几秒钟后,我不断收到“抱歉,发生服务器错误”。代码如下:

function getTextFromHtml(body) {
  return getTextFromNode(Xml.parse(body, true).getElement());
}


function getTextFromNode(x) {
  switch(x.toString()) {
    case 'XmlText': return x.toXmlString();
    case 'XmlElement': return x.getNodes().map(getTextFromNode).join('');
    default: return '';
  }
}

function searchy() {
  var search = GmailApp.search('"soundcloud.com" newer_than:1d');
  for (var i=0; i < search.length; i++) {
    var msgs = search[i].getMessages();
    for (var j=0; j < msgs.length; j++) {
      var body = msgs[j].getPlainBody();
      var printthis = getTextFromHtml(body);
      var regex = /https?:\/\/soundcloud\.com\/\S*/gi;
      var scURL = regex.exec(printthis);
      var sheet = SpreadsheetApp.getActiveSheet();
      sheet.appendRow(scURL);
      }
    }
  }

这是执行记录:

[15-03-05 11:16:59:009 CST] Starting execution
[15-03-05 11:16:59:539 CST] GmailApp.search(["soundcloud.com" newer_than:1d]) [0.512 seconds]
[15-03-05 11:16:59:724 CST] GmailThread.getMessages() [0.184 seconds]
[15-03-05 11:16:59:929 CST] GmailMessage.getPlainBody() [0.205 seconds]
[15-03-05 11:16:59:971 CST] Xml.parse([https://soundcloud.com/josef-salvat/josef-salvat-every-night-1

-- 
(EMAIL SIGNATURE)
, true]) [0.042 seconds]
[15-03-05 11:16:59:973 CST] XmlDocument.getElement() [0 seconds]
[15-03-05 11:16:59:977 CST] XmlText.toXmlString() [0 seconds]
[15-03-05 11:16:59:977 CST] SpreadsheetApp.getActiveSheet() [0 seconds]
[15-03-05 11:17:00:100 CST] Sheet.appendRow([[https://soundcloud.com/josef-salvat/josef-salvat-every-night-1]]) [0.122 seconds]
[15-03-05 11:17:00:280 CST] GmailThread.getMessages() [0.179 seconds]
[15-03-05 11:17:04:388 CST] GmailMessage.getPlainBody() [4.108 seconds]
[15-03-05 11:17:04:428 CST] Xml.parse([https://soundcloud.com/denniskruissen/de-hofnar-dennis-kruissen-ayo-technology-bootleg

-- 
(EMAIL SIGNATURE)
, true]) [0.039 seconds]
[15-03-05 11:17:04:429 CST] XmlDocument.getElement() [0 seconds]
[15-03-05 11:17:04:431 CST] XmlText.toXmlString() [0 seconds]
[15-03-05 11:17:04:431 CST] SpreadsheetApp.getActiveSheet() [0 seconds]
[15-03-05 11:17:04:432 CST] Sheet.appendRow([null]) [0 seconds]
[15-03-05 11:17:04:434 CST] Execution failed: We're sorry, a server error occurred. Please wait a bit and try again. (line 24, file "Copy of search") [5.406 seconds total runtime]

谁能帮忙???另外,有没有更好的方法来做到这一点?我是新手。

谢谢! 尼尔

编辑 我想到了。不得不在正则表达式函数中删除 g 。在这里查看 Henrique 的回答:Google Apps Script Regex exec() returning null 非常感谢文森特的帮助!

【问题讨论】:

    标签: javascript regex google-apps-script


    【解决方案1】:

    似乎 regex.exec() 正在返回 null,因为没有匹配项。

    修复函数以防止错误的一种方法:

    function searchy() {
      var search = GmailApp.search('"soundcloud.com" newer_than:1d');
      for (var i=0; i < search.length; i++) {
        var msgs = search[i].getMessages();
        for (var j=0; j < msgs.length; j++) {
          var body = msgs[j].getPlainBody();
          var printthis = getTextFromHtml(body);
          var regex = /https?:\/\/soundcloud\.com\/\S*/gi;
          var scURL = regex.exec(printthis);
    
          if (scURL !== null) {
              var sheet = SpreadsheetApp.getActiveSheet();
              sheet.appendRow(scURL);
              }
          }
        }
      }
    

    来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec

    【讨论】:

    • 感谢您的帮助,但现在它只是忽略了一些 soundcloud url。那是因为正则表达式的格式不正确吗?以下是它匹配的几个 soundcloud 示例:soundcloud.com/thump/saint-wknd-guest-mix、soundcloud.com/josef-salvat/josef-salvat-every-night-1,这是一个不匹配的示例:soundcloud.com/goldroom/…
    • 正则表达式看起来不错,如果您愿意,可以在regexpal.com 进行测试。我得到了这两个字符串的匹配项。您确定输入,并且 getTextFromNode() 函数按预期工作吗?我不知道你是否可以交互式调试,但你至少应该能够添加一些额外的日志输出来验证输入字符串是否符合预期。
    • 我使用了这个线程中的函数:stackoverflow.com/questions/20644301/… 正如我所说的,我对 javascript 和一般编码很陌生,所以我尝试使用这些函数并更改 var 以匹配我自己的。跨度>
    • 既然我想通了,我现在需要将'newer_than:'函数设置为分钟而不是天、周、月或年。对此有何建议?我尝试做 newer_than:10m、newer_than:.007d 等,但没有奏效。我应该把它变成一个新线程吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2010-11-17
    相关资源
    最近更新 更多