2

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.

6
  • I don't understand how the scenes array is coming from the chapters array. They seem to have nothing in common. Also chapters appears to be a nested array but I can't tell if thats intentional Commented Feb 28, 2013 at 19:20
  • Adding a bunch of console.log(typeof varbar) statements would be useful. As you say, something is not a String that you want to be a String. Isolate that and your question becomes much simpler. Commented Feb 28, 2013 at 19:23
  • more useful than that would be actually walking through the code with your browser's debugger so you can inspect exactly whats happening :) Commented Feb 28, 2013 at 19:26
  • try using the toString() method. The way you have it, is that you have an array - Scenes, then you are splitting it which creates an array and pushing into an array.. so yea, it makes sense why you are facing the issue you are. Commented Feb 28, 2013 at 19:37
  • when you write scenes.push(text.split("*"));, you are pushing an array in scenes, not a string. If you want to split the variable called text (which is an array) you have to iterate in it too. May I suggest you post a part of the text you want to parse so that one can test and suggest a working code ? Commented Feb 28, 2013 at 21:28

1 Answer 1

1

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]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.