将数组中的字符串拆分为子数组

时间:2013-02-28 19:15:45

标签: javascript google-apps-script

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

我收到错误:

  

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"]]

问题在于,当我将章节文档分割成场景时,似乎是将场景作为对象而不是字符串推入数组中,这使得无法在场景数组中进一步分割字符串。

1 个答案:

答案 0 :(得分: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]]