什么会导致slice()工作,但splice()失败?

时间:2018-03-05 17:48:27

标签: javascript google-apps-script

我正在处理包含职位描述信息的工作表,其中包括概述和职责。源数据不准确,但它在列的一个单元格中大致具有“概览”,并且在同一列中的其他单元格中具有每个“责任”。我在一个结果列中编写了概述(我根据字符数确定),并将每个职责构建一个无序列表到另一个结果列中。

我的来源并不总是完美的。我有这样的情况,第一个责任包含在与其相应的概述相同的单元格中。我可以通过文本认识到,并编写了一个indexOf()语句来做到这一点。

当我使用slice()方法时,脚本正确地指示在适当的索引之后发生的文本。但我需要的是使用splice()方法,以便我可以在创建结果数据之前从源数据中删除该文本。但是,当我将语句从slice()更改为splice()时,我收到一个错误:“ TypeError:无法找到对象中的函数拼接{单元格的开头文本}

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
}

}

显然还有很多事情要做(定义var数据,var writeRow,启动等),这些都可以正常工作。我确定我只是在某个地方做个白痴。但有人可以帮我弄清楚为什么slice()在这里工作,但splice()不是吗?

相关问题