0

I am working on a sheet that contains job description information, including an Overview and Responsibilities. The source data is not exact, but it roughly has the Overview in one cell of a column, and each Responsibility in additional cells in the same column. I am writing the Overview (which I am determining based on character count) in one results column, and building an unordered list with each the Responsibilities into another results column.

My source isn’t always perfect though. I have situations where the first Responsibility is included in the same cell as its corresponding Overview. I can recognize that by the text, and have an indexOf() statement written to do that.

When I use a slice() method, the script is correctly indicating the text that is occurring after the appropriate index. But what I need is to use the splice() method, so that I can remove that text from the source data before creating the results data. However, when I change the statement from slice() to splice(), I’m getting an error: “TypeError: Cannot find function splice in object {beginning text of the cell}

for(i=0; i<data.length; i++) {
var iRow = data[i];
if(iRow[12].length > 250) {  // this is an overview
  if(iRow[12].indexOf("What you’ll do")>-1) {  // is there a responsibility at the end of the overview?
    var startIndex = iRow[12].indexOf("What you’ll do");

    // this is the line that works for slice(), but not splice()
    var txt = iRow[12].splice(startIndex, 26);  // splice out the end of text, starting at the index.

    data[writeRow][18] += iRow[12];  // write the overview, without the added responsibility
    data[writeRow][19] += "<li>" + txt + "</li>";  // add the extracted responsibility to its list
  } else {  // these is no responsibility added to the end of the overview
    data[writeRow][18] += iRow[12];  // write the overview
  }
} else { // this is a responsibility
  data[writeRow][19] += "<li>" + iRow[12] + "</li>";  // add it to the list
}

}

There's obviously a lot more going on (defining var data, var writeRow, initiating the , etc) which all works fine. I’m sure that I’m just being an idiot somewhere. But can someone help me figure out why slice() works here, but splice() doesn’t?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Andrew
  • 417
  • 5
  • 21
  • `splice` only works on arrays, not strings? https://stackoverflow.com/questions/20817618/is-there-a-splice-method-for-strings – Kevin Hoerr Mar 05 '18 at 20:09

1 Answers1

2

splice is an array function. slice is both, an Array function and a String function.

References

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    To add to this answer, strings are immutable so it would make sense that strings do not support `splice`. – Clint Mar 05 '18 at 20:13
  • Like I said, I'm sure I was just being an idiot somewhere. I had done a decent amount of digging into slice() vs splice(), but nothing I had looked at broke it down like that for me. Now I get why it wasn't working. Thanks Ruben and Kevin. – Andrew Mar 05 '18 at 22:32