2

我在尝试将数组中的字符串拆分为子数组然后将其推回原始数组时遇到问题。

我得到错误:

TypeError:在对象中找不到函数拆分

这是我的代码:

// Split chapter into scenes
for(c in files){
  var ID = files[c].getId();
  var chapter = DocumentApp.openById(ID);
  var text = chapter.getText();

  // Splits chapter into scenes and pushes them to scenes
  scenes.push(text.split("*"));

  // Splits scenes into n,s,b
  for(s in scenes){
    scenes[s] = scenes[s].split("~");
  }

  // Push in scene wordcount
  for(b in scenes){
    var wordCount = scene[b][2].split(" ");
    scenes[b].push(wordCount.length);
  }
}

脚本导入的章节文档格式如下:

场景标题
~场景
概要
~场景
主体……是的。

*

到银河系
~
就这样……
~
就这样,一切都是这样那样。对。

*

结束
~
这就是结束。
~
所以事情就是这样,一切都是这样那样。对。结束。

我有两个数组,一个称为chapters包含章节号、名称和总字数的数组,然后是场景,它包含特定章节中的所有场景(也细分为场景名称、摘要、正文和字数)。章节标题使用 DocsList 函数导入到章节数组中。

我从驱动器中获取作为章节的特定文档中的文本,并首先使用字符 * 将其拆分为场景。

然后,在一个完美的世界中,我将逐步通过场景数组将对象数组中的每个字符串拆分为 s 子数组,该子数组使用字符 ~ 将场景编号、场景名称和场景主体分隔在一个子数组中,然后将该数组推入原始数组。

因此,如果

scenes = ["The Beginning ~ In the beginning, blah blah blah ~ Body of the scene goes here", "The Beginning ~ In the beginning, blah blah blah ~ Body of the scene goes here"]

处理后会变成:

scenes = [["The beginning", "In the beginning, blah blah blah", "Body of the scene goes here"], ["The beginning", "In the beginning, blah blah blah", "Body of the scene goes here"]]

问题是当我将章节文档拆分为场景时,它似乎将场景作为对象而不是字符串推送到数组中,这使得无法进一步拆分场景数组中的字符串。

4

1 回答 1

1

这样的事情会做你想要的吗?

function test(){ // replace this with the DocumentApp that gets your chapters
var chapter = "Scene Title~Summary of scene~Body of the scene...yeah.*To the Galaxy~And so it goes...~And so it goes like this that everything was this and that. Right.*The End~This is the end.~And so it goes like this that everything was this and that. Right. The end."
Logger.log(splitText(chapter))
}


function splitText(chapter){
  var temp = []
  var text = []
  var scenes=[]
  var wordCount
  // Splits chapter into scenes and pushes them to scenes
  temp=(chapter.split("*"));

  for(t in temp){
    text = temp[t].split("~")
    Logger.log(t+'  '+text)
    wordCount = temp[t].split(" ").length
    text.push(wordCount)
    scenes.push(text)
    }
 return scenes  
}

日志结果:

0  Scene Title,Summary of scene,Body of the scene...yeah.
1  To the Galaxy,And so it goes...,And so it goes like this that everything was this and that. Right.
2  The End,This is the end.,And so it goes like this that everything was this and that. Right. The end.
[[Scene Title, Summary of scene, Body of the scene...yeah., 7.0], [To the Galaxy, And so it goes..., And so it goes like this that everything was this and that. Right., 18.0], [The End, This is the end., And so it goes like this that everything was this and that. Right. The end., 19.0]]
于 2013-03-01T06:28:19.017 回答