Loop script for all values of dropdown - loops

I'm looking to make a script that cycles through a dropdown list and creates a pdf for each.
https://docs.google.com/spreadsheets/d/1HrXWkNXT7aEWOXkngiuSX9Sr1F0V4Y_rZH6Eg3mjaJQ/edit?usp=sharing
First I would like to check if B2 is not empty, then if so create pdf and change A2 to the next option until all are complete. I have a basic script but feel free to disregard!
function loopScript() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const interface = ss.getSheetByName("Interface");
var folderID = "###GOOGLE DRIVE FOLDER ID###";
var folder = DriveApp.getFolderById(folderID);
const exportOptions = 'exportFormat=pdf&format=pdf'
+ '&size=A4'
+ '&portrait=true'
+ '&scale=4'
+ '&fith=true&source=labnol'
+ '&top_margin=0.05'
+ '&bottom_margin=0.05'
+ '&left_margin=1.00'
+ '&right_margin=0.25'
+ '&sheetnames=false&printtitle=false'
+ '&pagenumbers=false&gridlines=false'
+ '&fzr=false'
+ '&gid=125740569';
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
const nameFile = "NAME OF FILE" + ".pdf" ;
folder.createFile(response.setName(nameFile));
DriveApp.createFile(response.setName(nameFile));
}

I believe your goal is as follows.
You want to check the cell "B2". When the cell "B2" is not empty, you want to set the value of the dropdown list of cell "A2" to the next value of the list.
For example, when the dropdown list is Joe, Barry, Jane, Fred and the cell "A2" is Barry, you want to set the cell to Jane.
In this case, how about the following modified script?
Modified script:
From:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const interface = ss.getSheetByName("Interface");
To:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const interface = ss.getSheetByName("Interface");
if (interface.getRange("B2").isBlank()) return;
const range = interface.getRange("A2");
const values = [...new Set(range.getDataValidation().getCriteriaValues()[0].getValues().flat())];
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
In this modified script, when the cell "B2" is empty, the script is finished. When the cell "B2" is not empty, the script is run and the cell "A2" is updated and your script for creating the PDF file is run.
Note:
In above modified script, when the dropdown list is Joe, Barry, Jane, Fred and the cell "A2" is Fred, the value of Joe is set. If you want to change this, please modify the above script.
In your current script, url is not defined. Please be careful this.
References:
isBlank()
getDataValidation()
getCriteriaValues()

Issue:
If I understand you correctly, you want to do the following:
For each dropdown in A2, check if the formula in B2 populates any values (based on data from sheet Data).
If any value is populated in B due to the formula, create a PDF file using the value of A2 for the file name (you have achieved this already).
Method 1:
In this case, I'd suggest the following workflow:
Retrieve an array with the accepted values from A2 dropdown (you can use the method used in Tanaike's answer).
Iterate through these values, and for each one, set the A2 value, using Range.setValue.
Call flush in order to update the data in B2 according to the current value in A2.
Check if B2 is blank (using Range.isBlank, for example).
If B2 is not blank, create the drive file.
function loopScript() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const interface = ss.getSheetByName("Interface");
const range = interface.getRange("A2");
const values = [...new Set(range.getDataValidation().getCriteriaValues()[0].getValues().flat())].filter(String);
values.forEach(name => {
range.setValue(name);
SpreadsheetApp.flush();
if (!interface.getRange("B2").isBlank()) {
// CODE FOR CREATING FILE
}
});
}
Method 2:
In the previous method, setValue, flush, getRange and isBlank are used iteratively, greatly increasing the amount of calls to the spreadsheet. This is not the best practice, as it will slow down the script (see Minimize calls to other services), and this will get worse if there are more valid options for the dropdown.
Therefore, since the data this formula is using can be found in sheet Data, I'd suggest using that source data instead of the formula, in order to minimize the calls to the spreadsheet.
In this case, you could follow this workflow:
Get all data in Data at once using Range.getValues.
Get all valid options in the data validation from A2, as in method 1.
For each option, check if there's any row in Data that has this option in column A and a non-empty cell in B.
If there is some data for that option, create the file.
function loopScript() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const interface = ss.getSheetByName("Interface");
const data = ss.getSheetByName("Data");
const DATA_FIRST_ROW = 2;
const dataValues = data.getRange(DATA_FIRST_ROW,1,data.getLastRow()-DATA_FIRST_ROW+1,2).getValues();
const range = interface.getRange("A2");
const values = [...new Set(range.getDataValidation().getCriteriaValues()[0].getValues().flat())].filter(String);
values.forEach(name => {
const optionValues = dataValues.filter(dataRow => dataRow[0] === name);
const nonEmpty = optionValues.some(optionValue => optionValue[1] !== "");
if (nonEmpty) {
// CODE FOR CREATING FILE
}
});
}

Thanks to everyone's help, I ended up using Tanaike's advice and continued on to come up with this:
function sendAll() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var interface = ss.getSheetByName("Interface");
var range = interface.getRange("A2");
// Cell of validation
const values = [...new Set(range.getDataValidation().getCriteriaValues()
[0].getValues().flat())]; // Gets array of validation
var first = values[0];
// 1st cell of validation
var number = values.length - 1;
// Length of validation
range.setValue(first);
// Sets value to first one
for(i = 0;i < number;i++) {
// Loop number of names
if (interface.getRange("B2").getValue().length > 0) {
// Sheet isn't empty
var person = interface.getRange("A2").getValue();
const url = 'MY SHEET URL';
var folderID = "MY FOLDER ID";
var folder = DriveApp.getFolderById(folderID);
const exportOptions = 'exportFormat=pdf&format=pdf'
+ '&size=A4'
+ '&portrait=true'
+ '&scale=4'
+ '&fith=true&source=labnol'
+ '&top_margin=0.05'
+ '&bottom_margin=0.05'
+ '&left_margin=1.00'
+ '&right_margin=0.25'
+ '&sheetnames=false&printtitle=false'
+ '&pagenumbers=false&gridlines=false'
+ '&fzr=false'
+ '&gid=0';
var params = {method:"GET",headers:{"authorization":"Bearer "+
ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
const nameFile = person + ".pdf" ;
folder.createFile(response.setName(nameFile));
DriveApp.createFile(response.setName(nameFile));
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
}
else {const nextValue = values[values.indexOf(range.getValue()) + 1] ||
values[0];
range.setValue(nextValue);}
}
}

Related

Filtering an Array of Sheets on three criteria with output being the URL link to the filtered sheets

I am creating two lists of links on my opening sheet (9060DASH) in Google Sheets. It looks like this:
One list is links to all sheets that are not hidden (Active) and one to all that are hidden. Each item must meet two other criteria to be included:
The sheet name must not include the numbers "9060."
The cell GH3 in the sheet must contain "Protected."
I have a functioning script, but it is loading too slowly. Here is a sample to show how it loads during onOpen(). How can I do this more efficiently? Can it be done better using "push"? Here is the script:
function populateSheetList() {
SpreadsheetApp.getActive().getSheetByName("9060DASH").activate();
var ss = SpreadsheetApp.getActive();
var ui = SpreadsheetApp.getUi();
ss.getRange('9060DASH!D4:E100').clear();
var counter = 4;
var sheetName = "";
var cellID= "";
var richValue = "";
var sheetURL = ss.getUrl();
var sheetLink = ""
var mySheet = "";
var shouldNotContain = '9060'; //array code from stackover to build array without 9060 sheets
var sheetNames = SpreadsheetApp.getActiveSpreadsheet().getSheets().map(s => s.getName());
var filtered = sheetNames.filter(x => !x.toLowerCase().match(shouldNotContain.toLowerCase()));
/** CREATES ACTIVE SHEETS LIST */
for(var i =0;i<filtered.length;i++){
cellID = '9060DASH!D'+counter;
sheetName = filtered[i];
mySheet = ss.getSheetByName(sheetName);
sheetLink = sheetURL+'#gid='+ mySheet.getSheetId();
if(mySheet.isSheetHidden() == false){
if(ss.getRange(sheetName+"!GH3").getValue() == "Protected"){
richValue = SpreadsheetApp.newRichTextValue()
.setText(sheetName)
.setLinkUrl(sheetLink)
.build();
ss.getRange(cellID).setRichTextValue(richValue);
counter = counter+1;
}
}
}
/** GATHERING HIDDEN SHEETS */
var counter = 4;
for(var i =0;i<filtered.length;i++){
cellID = '9060DASH!E'+counter;
sheetName = filtered[i];
mySheet = ss.getSheetByName(sheetName);
sheetLink = sheetURL+'#gid='+ mySheet.getSheetId();
if(mySheet.isSheetHidden() == true){
if(ss.getRange(sheetName+"!GH3").getValue() == "Protected"){
richValue = SpreadsheetApp.newRichTextValue()
.setText(sheetName)
.setLinkUrl(sheetLink)
.build();
ss.getRange(cellID).setRichTextValue(richValue);
counter = counter+1;
}
}
}
}
I have reduced the time to load the dashboard by rearranging a number of things and by getting a final array before writing anything out to the dash. I reformatted the cells and sorted the records before the evaluation loops began. I added more status messages, to keep users engaged while the background work was going on. I put the criteria of separating active from hidden sheets last and, depending on which, wrote the values to two different arrays (actvSheets and hidnSheets). Then I processed each array out to the dashboard. I did this in for loops. I suspect I could save even more time to write them out as one command from each array, but I have not been able to figure out how to write out in vertical form. Here is the code. Any refinements are welcome!
/**
* =================
* populateSheetList
* =================
* This function builds the available sheets in the
* 9060DASH sheet--containing all with the Protected
* status.
*/
function populateSheetListNEW() {
msgDash("Clearing sheet list . . . ")
// SpreadsheetApp.getActive().getSheetByName("9060DASH").activate();
var ss = SpreadsheetApp.getActive();
var ui = SpreadsheetApp.getUi();
ss.getRange('9060DASH!D4:E100')
.clear()
.setFontSize(14)
.setHorizontalAlignment('left')
.sort({column: 4, ascending: true})
msgDash("Evaluating sheets . . . ")
var counter = 4;
var sheetName = "";
var cellID= "";
var richValue = "";
var sheetURL = ss.getUrl();
var sheetLink = ""
var mySheet = "";
var actvSheets = [];
var hidnSheets = [];
msgDash("Eliminating administrative sheets . . . ")
var shouldNotContain = '9060'; //array code from stackover to build array without 9060 sheets
var sheetNames = SpreadsheetApp.getActiveSpreadsheet().getSheets().map(s => s.getName());
var filtered = sheetNames.filter(x => !x.toLowerCase().match(shouldNotContain.toLowerCase()));
var sortFilt = filtered.sort();
/** CREATES SHEET ARRAYS */
msgDash("Gathering census worksheets . . . ")
for(var i =0;i<sortFilt.length;i++){
sheetName = sortFilt[i];
mySheet = ss.getSheetByName(sheetName);
if(ss.getRange(sheetName+"!GH3").getValue() == "Protected"){
if(mySheet.isSheetHidden() == false){
actvSheets.push(sheetName);
} else {
hidnSheets.push(sheetName);
}
}
}
msgDash("Gathering active sheets . . . ")
/** WRITING DATA FROM ACTVSHEETS ARRAY */
for(var j = 0; j < actvSheets.length; j++){
sheetName = actvSheets[j];
mySheet = ss.getSheetByName(actvSheets[j]);
sheetLink = sheetURL+'#gid='+ mySheet.getSheetId();
richValue = SpreadsheetApp.newRichTextValue()
.setText(sheetName)
.setLinkUrl(sheetLink)
.build();
cellID = '9060DASH!D' + counter;
counter = counter+1;
ss.getRange(cellID).setRichTextValue(richValue);
}
msgDash("Gathering hidden sheets . . . ");
/** WRITING DATA FROM HDDNSHEETS ARRAY */
counter = 4;
for(var k = 0; k< hidnSheets.length; k++){
sheetName = hidnSheets[k];
mySheet = ss.getSheetByName(hidnSheets[k]);
sheetLink = sheetURL+'#gid='+ mySheet.getSheetId();
richValue = SpreadsheetApp.newRichTextValue()
.setText(sheetName)
.setLinkUrl(sheetLink)
.build();
cellID = '9060DASH!E' + counter;
counter = counter+1;
ss.getRange(cellID).setRichTextValue(richValue);
}
msgDash('Dashboard ready. Enjoy your census work!');
ss.getRange('9060DASH!D2').setValue("For hidden sheets, respond to 'Unhide' instruction. Click Refresh button to rebuild list.");
Utilities.sleep(20);
msgDash(''); //clears messages
}

script & sheet timing out when trying to print large arrays in google script

Background
I have a function that makes a REST API call using UrlFetchApp in Google Scripts.
But the response only returns 2000 records at a time. If there are more records, there is, in the response, a key called nextRecordsUrl, which contains the endpoint and parameters needed to get the next batch of records.
I use a do...while loop to iterate through, pushing the records into a predesignated array, make the next api call. And when it reaches the last batch of records, it exists the do-while loop, then prints (not sure if that's the right term here) the entire to a Google Sheet.
The code
It looks like this:
function getCampaignAssociations() {
clearPage('CampaignAssociations');
var query = '?q=select+CampaignMember.FirstName,CampaignMember.LastName,CampaignMember.LeadId,CampaignMember.ContactId,CampaignMember.Name,CampaignMember.CampaignId,CampaignMember.SystemModstamp,CampaignMember.Email+from+CampaignMember+ORDER+BY+Email ASC,SystemModstamp+ASC';
try {
var arrCampAssociation = getInfoByQuery(query);
if (arrCampAssociation.records.length < 1) {
throw 'there are no records in this query';
}
var campaignAssoc = [];
do {
Logger.log(arrCampAssociation.nextRecordsUrl);
for (var i in arrCampAssociation.records) {
let data = arrCampAssociation.records[i];
let createDate = Utilities.formatDate(new Date(data.SystemModstamp), "GMT", "dd-MM-YYYY");
let a1 = "$A" + (parseInt(i) + 2);
let nameFormula = '=IFERROR(INDEX(Campaigns,MATCH(' + a1 + ',Campaigns!$A$2:A,0),2),"")';
let typeFormula = '=IFERROR(INDEX(Campaigns,MATCH(' + a1 + ',Campaigns!$A$2:A,0),3),"")';
campaignAssoc.push([data.CampaignId, nameFormula, typeFormula, data.Email, data.FirstName, data.LastName, data.LeadId, data.ContactId, createDate]);
}
var arrCampAssociation = getQueryWithFullEndPoint(arrCampAssociation.nextRecordsUrl);
} while (arrCampAssociation.nextRecordsUrl != null && arrCampAssociation.nextRecordsUrl != undefined);
let endRow = campAssocSheet.getLastRow(),
endColumn = campAssocSheet.getLastColumn(),
nameRange = campAssocSheet.getRange(2, 1, endRow, endColumn),
destRange = campAssocSheet.getRange(2, 1, campaignAssoc.length, campaignAssoc[0].length);
destRange.setValues(campaignAssoc);
sheet.setNamedRange('CampaignAssociation', nameRange);
} catch (e) {
Logger.log(e);
Logger.log(arrCampAssociation);
Logger.log(campaignAssoc);
Logger.log(i);
}
}
Issue
Everything works nicely until it comes to printing the array campaignAssoc to the Google Sheet.
See screenshot of the log below. It contains the endpoint for the next both. Notice the timestamp between the earlier logs and the timestamp between the last endPoint and the log where it timed out.
It seems to me that the issue is that when it comes to the printing of the data, it's having issues. If that's the case, have I overloaded the array? There are a total of over 36400 records.
Second attempt
I've tried resetting the array at each loop and printing the array to Google sheet. This is just 2000 records at each attempt and I've definitely done more rows at 1 time, but that didn't help.
Here's the code for that attempt.
function getCampaignAssociations() {
clearPage('CampaignAssociations');
var query = '?q=select+CampaignMember.FirstName,CampaignMember.LastName,CampaignMember.LeadId,CampaignMember.ContactId,CampaignMember.Name,CampaignMember.CampaignId,CampaignMember.SystemModstamp,CampaignMember.Email+from+CampaignMember+ORDER+BY+Email ASC,SystemModstamp+ASC';
try {
var arrCampAssociation = getInfoByQuery(query);
if (arrCampAssociation.records.length < 1) {
throw 'there are no records in this query';
}
do {
Logger.log(arrCampAssociation.nextRecordsUrl);
var campaignAssoc = [];
for (var i in arrCampAssociation.records) {
let data = arrCampAssociation.records[i];
let createDate = Utilities.formatDate(new Date(data.SystemModstamp), "GMT", "dd-MM-YYYY");
let a1 = "$A" + (parseInt(i) + 2);
let nameFormula = '=IFERROR(INDEX(Campaigns,MATCH(' + a1 + ',Campaigns!$A$2:A,0),2),"")';
let typeFormula = '=IFERROR(INDEX(Campaigns,MATCH(' + a1 + ',Campaigns!$A$2:A,0),3),"")';
campaignAssoc.push([data.CampaignId, nameFormula, typeFormula, data.Email, data.FirstName, data.LastName, data.LeadId, data.ContactId, createDate]);
}
let lastRow = campAssocSheet.getLastRow()+1;
campAssocSheet.getRange(lastRow,1,campaignAssoc.length,campaignAssoc[0].length).setValues(campaignAssoc);
var arrCampAssociation = getQueryWithFullEndPoint(arrCampAssociation.nextRecordsUrl);
} while (arrCampAssociation.nextRecordsUrl != null && arrCampAssociation.nextRecordsUrl != undefined);
let endRow = campAssocSheet.getLastRow(),
endColumn = campAssocSheet.getLastColumn(),
nameRange = campAssocSheet.getRange(2, 1, endRow, endColumn);
sheet.setNamedRange('CampaignAssociation', nameRange);
} catch (e) {
Logger.log(e);
Logger.log(arrCampAssociation);
Logger.log(campaignAssoc);
Logger.log(i);
}
}
So here, each loop took a lot longer. Instead of being 1-2 seconds between each loop, it took 45 seconds to a minute between each and timed out after the 4th loop. See the log below:
How do I fix this?

Parse Json Array using Google Scripts

I am trying to parse this JSON Array but I'm not familiar with how to loop through a JSON in Google scripts. I'm getting an Undefined error for the price variable in this API.
API link.
function CBAPI() {
// Link the script with a spreadsheet using the unique identifier found in the spreadsheet web address
var ss = SpreadsheetApp.openById('16UqqC_MjnRfwbpREUcrcl7q69bUjzPgoUm6ZBMorizk');
var APIPullSheet = ss.getSheetByName("APIPull");
// Clear Columns A, B, C & D
APIPullSheet.getRange('A2:F19999').clearContent();
var url= "https://api.coinmarketcap.com/v2/ticker/132";
var responseAPI = UrlFetchApp.fetch(url);
var parcedData = JSON.parse(responseAPI.getContentText());
var id = [];
var price = [];
id.push(['id']);
price.push(['price']);
id.push([parcedData.data.id]);
price.push([parcedData.data.price]);
idRange = APIPullSheet.getRange(1, 1, id.length, 1); // Put isFrozen in column A
idRange.setValues(id);
priceRange = APIPullSheet.getRange(1, 2, price.length, 1); // Put lowestAsk in column B
priceRange.setValues(price);
// Append Latest Data to End of the File
var tableData = ss.getSheetByName("TableData");
var rangeData = tableData.getRange("H1:K1");
var latestData = rangeData.getValues(); // Put I1 to O1 in latestData variable
tableData.appendRow(latestData[0]); // Put the data at the bottom of the spreadsheet
// Keep 144 rows - Delete any extra starting at row 2
var rowsToKeep = 5000; // 5000 at request of Edwin
var totalRows = tableData.getLastRow();
var numToDelete = totalRows - rowsToKeep;
if (numToDelete > 0)
{
tableData.deleteRows(2, numToDelete); // Purge Extra Rows - Starting With Row 2 (oldest)
}
}
The API returns price inside the quotes object.
Replace:
price.push([parcedData.data.price]);
With:
price.push([parcedData.data.quotes.USD.price]);

Accessing Items in an Array - AS3

I've got an Object item.movieimage that contain some texts (item:Object) that is retrieve from my database. The text is changing every week automatically.
If I do trace(item.movieimage) the output is something like this :
text1
text2
text3
text4
The words changes as the code is taking them from my database. The database changes the words every week.
Now, I want my AS3 code to display the third element of item.movieimage.
I've tried to do this :
var my_str:String = item.movieimage+",";
var ary:Array = my_str.split(",");
trace(ary[2]);
But it's not working. The output is "undefined".
Do you know how can I access a specific item in the Array that I've created ? Or how can I access the third item of item.movieimage ?
If I do trace(ary);, the output is :
text1,
text2,
text3,
text4,
EDIT :
For infos :
trace(typeof(item.movieimage)) and trace(typeof(ary)) are :
typeof(item.movieimage)=string
typeof(ary)=object
EDIT 2 :
Here's a screen capture of item.movieimage
Screen Cap of item.movieimage
EDIT 3
Here's my code in order to understand how "item.movieimage"is working
//Variables for downloading content from my database to my AS3 code
var urlReqSearchAll: URLRequest = new URLRequest("http://www.myWebSite/searchMovie4.php");
var loader5:URLLoader = new URLLoader();
//downloading content
function searchAll():void {
if (contains(list)){
list.removeChildren();
}
urlReqSearchAll.method = URLRequestMethod.POST;
loader5.load(urlReqSearchAll);
loader5.addEventListener(Event.COMPLETE,complete);
var variables:URLVariables = new URLVariables();
}
//Content Downloaded.
function complete(e:Event):void {
addChild(list);
products = JSON.parse(loader5.data) as Array;
hourSaved.data.saved=loader5.data;
products.reverse();
for(var i:int = 0; i < products.length; i++){
createListItem(i, products[i]);
}
displayPage(0);
showList();
}
// If too much items --> creates multiple page
const itemsPerPage:uint = 7;
var currentPageIndex:int = 0;
function displayPage(pageIndex:int):void {
list.removeChildren();
currentPageIndex = pageIndex;
var firstItemIndex:int = pageIndex * itemsPerPage;
var j:int = 0;
var lastItemIndex: int = firstItemIndex + 7; // as lastItemIndex should be 10 more
if (lastItemIndex > products.length) // if lastindex is greater than products length
lastItemIndex = products.length;
for(var i:int = firstItemIndex; i< lastItemIndex; i++){
createListItem( j, products[i]); // j control the position and i points to particular element of array..
j++;
}
next.visible = lastItemIndex < products.length - 1;
if(currentPageIndex==0){
previous.visible=false;
}
}
// Display the information downloded from my database
function createListItem(index:int, item:Object):void {
var listItem:TextField = new TextField();
var myFormat:TextFormat = new TextFormat();
myFormat.size = item.title.length > 13 ? 22 : 26
listItem.multiline = true;
listItem.wordWrap = true;
myFormat.align = TextFormatAlign.CENTER;
myFormat.color = 0xA2947C;
myFormat.font = "Ebrima";
myFormat.bold = true;
listItem.defaultTextFormat = myFormat;
listItem.x = 135;
listItem.y = 123+ index * 84;
listItem.width = 200;
listItem.height = 80;
listItem.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
showDetails(item);
});
list.addChild(listItem);
str = item.title;
}
My php file "SearchMovie4.php" is like this :
$products = array();
while ($row = mysql_fetch_array($sql_result)) {
$products[] = array(
"title" => $row["theTitle"],
"movieimage" => $row["movieimage"],
"movielength" => $row["movielength"],
"story" => $row["story"],
);
}
echo json_encode($products);
So, if I do trace(item.movieimage) in AS3 code, it will display all the items in the row movieimage of my database.
If I do trace(item.title) in AS3 code, it will display all the items in the row title of my database.
What I'd like is to be able to do, in my AS3 code, trace(item.movieimage[2]) in order to show me the third item in the row "movieimage".
Well, the text you've provided to the movieimage property of the item object has not the same delimiter as what you have provided to the split() method; in your case the character of comma!
I suspect the delimiter you have, is the "space" character or a new-line character, like the character of carriage retrun.
In the following code snippet, I've used the "space" as the delimiter character:
var item:Object = new Object();
item.movieimage = "text1 text2 text3 text4";
var my_str:String = item.movieimage;
var ary:Array = my_str.split(" ");
trace(ary[2]); // text3
As #someOne said the output shows the list items are each on a new line, so you need to split() against newlines. One of these should work:
// if server uses "\n" newline char
var movies:Array = item.movieimage.split("\n");
// if server uses "\r" carriage return char
var movies:Array = item.movieimage.split("\r");
// if you aren't sure or there's a mixture of newline chars
var movies:Array = item.movieimage.split(/[\r|\n]+/);
Also note that if you have any control over how the server returns these values, you should just return a JSON array (since I see in your screenshot you are already decoding JSON), then you won't have to do any string splitting.

Search data in mongodb according to field values

I am using mongodb and node.js. In my database i have 6 fields that is
bid
color (Array)
size (Array)
cat_id
sub_cat_id
All is working fine. Now i want to add filter in my code. In filter area i have add this all fields. user select multiple colors and sizes so it will come in Array format but most of the time user will not select color option or size option at that time field values comes blank so my filter will not take any result from database. so i want to remove color or size field if value is empty during search. I have tried below code but its not working.how i do this.
var catId = new Array();
var sort = saveFilterSort.sort;
var filter = req.body;
if(req.body.catId){
catId.push("category_id:"+req.body.catId);
}
if(req.body.subcatid){
catId.push("sub_category_id:"+req.body.subcatid);
}
if(req.body.minprice){
catId.push("price:{$gt:"+req.body.minprice+"}");
}
if(req.body.maxprice){
catId.push("price:{$lt:"+req.body.maxprice+"}");
}
if(req.body.color){
catId.push("color:{$in:"+req.body.color+"}");
}
if(req.body.size){
catId.push("attribute:{$in:"+req.body.size+"}");
}
var finalCat = catId.join(',');
console.log(finalCat);
console.log(catId);
if((filter) && (sort)){
Product.find(
{
brand_id:bid, finalCat
},
function(error,fetchallFeatProds)
{
console.log('#######################');
console.log(fetchallFeatProds);
console.log('#######################');
callback(error,fetchallFeatProds);
}).sort( {_id:-1,price:-1} );
This code is not working. Please help me.
Mongoose find prototype handle json and not string
var query = {brand_id:bid};
var sort = saveFilterSort.sort;
var filter = req.body;
if(req.body.catId){
query.category_id = req.body.catId;
}
if(req.body.subcatid){
query.sub_category_id = req.body.subcatid;
}
if(req.body.minprice){
query.price = {$gt:req.body.minprice};
}
if(req.body.maxprice){
query.price = {$lt:req.body.maxprice};
}
if(req.body.color){
query.color = {$in:req.body.color};
}
if(req.body.size){
query.attribute = {$in:req.body.size};
}
if((filter) && (sort)){
Product.find(query, ...

Resources