Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot find function replace" errror when trying to replace part of a string

I need a way to replace part of a string with another string using Google Apps Script. I tested the following and it worked:

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;
}

But, when I embedded into the following code, it did not work. Any idea?

  //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);
    }
  }

The script did not work as I wish. It seems to stop at line:

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

Additional information: Google notified me with the following message

onFormSubmit

TypeError: Cannot find function replace in object https://plus.google.com/117520727894266469000. (line 536) formSubmit

like image 832
user2019913 Avatar asked Jan 28 '13 23:01

user2019913


1 Answers

I found the mistake. It is Type Error. value in the second block is not a "string", while the first block is a "string". Hence to fix the second block, I need to use:

var value = value.toString();

before passing to googlePlus(value)

like image 157
user2019913 Avatar answered Sep 22 '22 15:09

user2019913