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.
console.log(typeof varbar)statements would be useful. As you say, something is not aStringthat you want to be aString. Isolate that and your question becomes much simpler.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 ?