nellygrl
nellygrl

Reputation: 657

split a string in an array into a sub array

I am having an issue trying to split a string in an array into a sub array and then push it back into the original array.

I get the error:

TypeError: Cannot find function split in object

Here is my code:

// 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);
  }
}

The chapter document the script imports is formatted as follows:

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.

I have two arrays, one called chapters that holds the chapter number, name and total word count and then scenes, which hold all the scenes in a particular chapter (also subarrayed into scene name, summary, body and wordcount). The chapter titles are imported into the chapters array using DocsList functions.

I grab the text in a particular document that is a chapter from drive and first split it using the character * into scenes.

Then, in a perfect world, I would step through the scene array splitting each string in the object array into s sub array that seperates the scene number, scene name and scene body in a sub array using the character ~ and then push that array into the original array.

So, if

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

After processing it, it would change to:

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

The issue is that when I split the chapter document into scenes, it seems to be pushing the scenes into the array as objects instead of strings, which makes it impossible to further split the strings in the scenes array.

Upvotes: 2

Views: 3308

Answers (1)

Serge insas
Serge insas

Reputation: 46802

Would something like that do what you want ?

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  
}

Log result :

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

Upvotes: 1

Related Questions