【问题标题】:"Cannot find function replace" errror when trying to replace part of a string尝试替换部分字符串时出现“找不到函数替换”错误
【发布时间】:2013-01-28 23:29:01
【问题描述】:

我需要一种使用 Google Apps 脚本将字符串的一部分替换为另一个字符串的方法。我测试了以下内容并成功:

function test(){
  var value = 'https://plus.google.com/117520727894266469000';
  var googleimageURL = googlePlus(value);
  Logger.log('Returned Result: ' + googleimageURL);
}

function googlePlus(value){
    var apiKey = ScriptProperties.getProperty('apiKey');
    var re = 'http://'
    var theURL = value;
    Logger.log('Google+ is called: ' + value);
    var replacingItem = 'https://';
    var theURL = theURL.replace(re, replacingItem);
    Logger.log('Google+ Values 2: ' + value + ' : ' + theURL + ' after ' + replacingItem);
    return imageURL;
}

但是,当我嵌入以下代码时,它不起作用。有什么想法吗?

  //If a Google+ column was not specified,put a flag.
  if (googleColumn && column == googleColumn) {
    if (value == ""){
      columns.push(nogoogleColumn);
      values.push(flag);
      var googleimageURL="";
    } else {
      var googleimageURL = googlePlus(value);
      Logger.log('Returned Result: ' + googleimageURL);
    }
  }

脚本没有按我希望的那样工作。它似乎停在以下行:

var theURL = theURL.replace(re, replacingItem);

附加信息:Google 通过以下消息通知我

提交表单

TypeError:在对象 https://plus.google.com/117520727894266469000 中找不到函数替换。 (第 536 行)表单提交

【问题讨论】:

    标签: javascript google-apps-script triggers


    【解决方案1】:

    我发现了错误。这是类型错误。第二个块中的value 不是“字符串”,而第一个块是“字符串”。因此,要修复第二个块,我需要使用:

    var value = value.toString();
    

    在传递给googlePlus(value)之前

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2019-03-16
      • 2021-09-26
      • 2013-02-02
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      相关资源
      最近更新 更多