简体   繁体   中英

Scripts - Find & Replace in Google Sheets

I have a JavaScript function pulling data from a webpage and placing the data in A,B,C,D of Google Sheets. Google Scripts Scraper I would like to use something that takes the value 'MKE' in column C, and replace it Milwaukee, WI still leaving the address.

I ran across this script:

if (addressParts[1] == "MKE" || addressParts[1] === undefined) {
            addressParts[1] = "Milwaukee";
        } else if (addressParts[1] == "WMW") {
            addressParts[1] = "West Milwaukee";
        } else if (addressParts[1] == "WA") {
            addressParts[1] = "West Allis";

But am unaware if this script will work with my project. Does anyone have any other suggestions for code to use?

I suggest this code:

function getReplaced(value)
{

  var replacer = 
  {
    undefined: "Milwaukee",
    MKE: "Milwaukee",
    WMW: "West Milwaukee",
    WA: "West Allis"
  }
  var result = replacer[value]
  return result || value;
}

function TESTreplacer()
{

  Logger.log(getReplaced(undefined));        // Milwaukee
  Logger.log(getReplaced('MKE'));            // Milwaukee
  Logger.log(getReplaced('WMW'));            // West Milwaukee  
  Logger.log(getReplaced('WA'));             // West Allis
  Logger.log(getReplaced('Turtle Power'));   // Turtle Power

}

You may add new values into replacer or even make it separate object with it's own method addValue

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