AS3: Remove and add to variable after a character - actionscript-3

I am pulling data from a php script that gives me the names of a person on facebook along with there ID in this format:
Person Name:123456789
I would like to know if there is a away to split after the ":" and add the number (ID) to on array and the person name to another array.
Thanks for any help
Eli

Something like this:
var personArray:Array = [];
var idArray:Array = [];
var stringToSplit:String = "Person Name:123456789";
var splitArray:Array = stringToSplit.split(":");
personArray.push(splitArray[0]);
idArray.push(splitArray[1]);
trace(personArray); // outputs "Person Name"
trace(idArray); // outputs "123456789"

Related

How can i do send bulk of list within one E-mail on App Script?

I am trying to do send an email that will go to the customer and he or she will see that which product has arrived in the office,
First, I am gathering the values from my Sheet like that (part of that code);
var partReceivedQuerySheet = uyg.getSheetByName("Part Received Query");
var partReceivedQuerySheetLastRow = partReceivedQuerySheet.getLastRow();
var indexSupplierName = partReceivedQuerySheet.getRange("A2:A"+partReceivedQuerySheetLastRow).getValues();
I am using the MailApp function, I want to send that email like table, for that reason I am using
HtmlBody, and my code like that (part of that code);
'<table>'+
'<tr>'+
'<td>'+indexSupplierName+'</td>'+
'</tr>
I tried for loop before MailApp function but that time it is sending number of suppliers name mails,
I tried the For loop before MailApp, but at that time it sent as many e-mails as the number of suppliers one by one.
Based on Ron M answer I did Supplier Names like my input. My code is;
var indexSupplierNames =partReceivedQuerySheet.getRange("A2:A"+partReceivedQuerySheetLastRow).getValues().flat();
var indexProductNames =partReceivedQuerySheet.getRange("B2:B"+partReceivedQuerySheetLastRow).getValues().flat();
var productInfoTable = '<table border="2"><tr><th><b>Brand</b></th>';
productInfoTable+='<th><b>Product Name</b></th>';
productInfoTable+='<th><b>Quantity</b></th>';
productInfoTable+='<th><b>Arrived to Warehouse?</b></th></tr>';
indexSupplierNames.forEach(supplierName=> {
productInfoTable+='<tr><td>'+supplierName+'</td></tr>'
});
productInfoTable+= '</table>';
I have Product Names, Quantity, and one more column on My Sheet, I want to add all to the table, but when I try that forEach() thing I can not align Product Names with Supplier Names.
Finally, the output that I want to like this. I am sorry about that, I am noobie in AppScript but I tried really so much and I could not add it.
Based on your expected output. The supplier names' should be written on a different table rows <tr></tr>
Here is a sample code:
var partReceivedQuerySheetLastRow = partReceivedQuerySheet.getLastRow();
var indexSupplierName = partReceivedQuerySheet.getRange("A2:A"+partReceivedQuerySheetLastRow).getValues().flat();
Logger.log(indexSupplierName);
var email = 'youremail';
var hmtlContent = '<table border="1"><tr><td><b>Brand</b></td></tr>';
indexSupplierName.forEach(name => {
hmtlContent += '<tr><td>'+name+'</td></tr>';
});
hmtlContent+='</table>';
Logger.log(hmtlContent)
MailApp.sendEmail(email,"Test","",{htmlBody: hmtlContent});
What it does?
I changed the Range.getValues() return from 2-d array into a 1-d array.
Created a starting html table content
Loop all supplier names. Append each name on a different row in the html table content
Output:
(Update:)
How to create and update the table with multiple columns:
var indexSupplierNames = ["Supplier1", "Supplier2", "Supplier3"];
var indexProductNames = ['Product1', 'Product2', 'Product3'];
var productInfoTable = '<table border="2"><tr><th><b>Brand</b></th>';
productInfoTable+='<th><b>Product Name</b></th>';
productInfoTable+='<th><b>Quantity</b></th>';
productInfoTable+='<th><b>Arrived to Warehouse?</b></th></tr>';
for (var i=0; i<indexSupplierNames.length; i++){
productInfoTable+='<tr><td>'+indexSupplierNames[i]+'</td><td>'+indexProductNames[i]+'</td><td>0</td><td>No</td></tr>'
}
productInfoTable+= '</table>';
Logger.log(productInfoTable);
MailApp.sendEmail("your#email.com","Test","",{htmlBody: productInfoTable});
Output:
Finally, I solved it like that thank you to Ron M for help me to learn.
Code;
productInfoTables = '<table border="2"><tr><th><b>Brand</b></th>';
productInfoTables+='<th><b>Product Name</b></th>';
productInfoTables+='<th><b>Quantity</b></th>';
productInfoTables+='<th><b>Arrived to Warehouse?</b></th></tr>';
for (var k=2;k<=partReceivedQuerySheetLastRow;k++){
var supplierNames = partReceivedQuerySheet.getRange(k,1).getValue();
var productNames = partReceivedQuerySheer.getRange(k,2).getValue();
var qty = partReceivedQuerySheet.getRange(k,3).getValue();
var arrivedToWarehouse = partReceivedQuerySheet.getRange(k,4).getValue();
productInfoTables+= '<tr<td>'+supplierNames+'</td>'+'<td>'+productNames+'</td>'+'<td>'+qty+'</td>'+'<td>'+arrivedToWarehouse+'</td></tr>'
}

Google App Script - Can not insert group member

I'm a beginner ok... I'm trying to get multiple values of a cell memberEmail, then insert to mail group.
But I got an error is Invalid Input: memberKey.
var groupMail = 'test#gmail.com';
var listUserMails = [];
var userMails = JSON.stringify(sheet.getRange(4, 9).getValue());
listUserMails = userMails.split("\\n");
for (var i = 0; i < listUserMails.length; i++) {
var userEmail = listUserMails[i];
var member = {
email: userEmail,
role: 'MEMBER'
};
member = AdminDirectory.Members.insert(member, groupMail);
}
Update:
When I log value of an array listMemberMails, it seems that the array I received contains only 1 string. It's not the 3 strings as expected.
["nguyen_tien_nghiep#gmail.com, nguyen_hai_ninh#gmail.com, vu_xuan_lam#gmail.com"]
<blockquote class="imgur-embed-pub" lang="en" data-id="a/hMd1ckO"></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
Your problem comes from the JSON.stringify function. It assigns to the variable userMails the value
"nguyen_tien_nghiep#gmail.com\n nguyen_hai_ninh#gmail.com\n vu_xuan_lam#gmail.com"
whereby the doublequotes are considered part of the string. This is why, if you split userMails in three, it will give you the following three array entries:
listUserMails[0]: "nguyen_tien_nghiep#gmail.com
listUserMails[1]: nguyen_hai_ninh#gmail.com
listUserMails[2]: vu_xuan_lam#gmail.com"
You can easily confirm the latter by adding Logger.log(userEmail) inside of your for-loop. This is what inhibits you from creating three members with a valid email address (since only the second entry is a valid email address.
With this being said if you replace
var userMails = JSON.stringify(sheet.getRange(4, 9).getValue());
with
var userMails = sheet.getRange(4, 9).getValue();
and then adjust the separator to "\n" instead of "\n" your code should work correctly.
As a suggestion, you can debug your code by inserting logs (e.g. for listUserMails.length). This will help you to find errors.

I am unsure of how to follow the instructions

var Mary = '{ "height":1.9, "age":36, "eyeColor":"brown"}';
// use JSON.parse() to create an object 'objectMary':
var objectMary = JSON.parse(Mary);
Instructions:
I need to add an additional line after the existing code to log the value of Mary's age to the console.
What does this mean? How would I do this?
If I understand the question correctly, you would like to access Mary's age from the json.
var Mary = '{ "height":1.9, "age":36, "eyeColor":"brown"}';
var objectMary = JSON.parse(Mary);
Here, objectMary has the parsed json object and you can directly access properties from it like objectMary.age, objectMary.height, etc.
If you are trying this code out in javascript then you could log Mary's age as
var Mary = '{ "height":1.9, "age":36, "eyeColor":"brown"}';
var objectMary = JSON.parse(Mary);
console.log(objectMary.age);
Just add console.log(objectMary.age); in your existing code.
DEMO
var Mary = '{ "height":1.9, "age":36, "eyeColor":"brown"}';
var objectMary = JSON.parse(Mary);
console.log(objectMary.age);

Incorrect Range Height - Google Script

I'm trying to create a Google sheet script that will take a list of names and resend them to the Google Sheet. I have two columns of data, the first column contains a persons name. The second column contains multiple cells that the person in the first cell is inviting to a party. This creates a problem, the name in column 1 might be on row 2, but if they invite 20 people then column one has blank spaces rows 3-21. It may sound pointless right now to most of you, but I want to be able to sort the sheet alphabetically by the name of the person who did the inviting, AND be able to sort it into a separate sheet alphabetically by the name of the guest in Column 2, while still keeping the person who invited them tracked as well. This is the only way I could think of accomplishing the task.
I'm currently stuck on writing the array back to the sheet, I keep getting "Incorrect range height, was 1 but should be 339." I've figured out how to successfully get an array of data, filled exactly how I wanted it, but can't seem to get this part. I've searched through here and tried to implement the solutions I find, but have had no luck.
This is what I have come up with so far, and it works up until the setValues(
function inviteSorter() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var current = sheet.getSheets()[0];
var lastRow = current.getLastRow();
var rangeData = current.getRange(2,1,lastRow-1,3);
var numColumns = rangeData.getNumColumns();
// Logger.log(rangeData);
var info = rangeData.getValues();
var Name = {};
// Examines the cell in the first column, if it is empty replaces it with the name from the previous cell.
for ( var i = 0; i< info.length; i++){
if (typeof(info[i][0]) == "string" && info[i][0] == ""){
Name[i] = Name[i-1];
} else{
Name[i] = info[i][0];
}
}
var data = []
for (var i = 0; i<lastRow-1; i++){
data.push(Name[i]);
}
var writeRange = current.getRange(2,1,data.length,1);
writeRange.setValues([data]);
The value you are expecting should be a 2D array, 1 column of multiple rows. What you get when using data.push(Name[i]); is a simple array of strings.
Try this way : data.push([Name[i]]); this will return an array of arrays and should satisfy the conditions for setValues(data)
( don't forget to remove the brackets in your last setValues statement )

Editing HTML5 LocalStorage through a function

I want to be able to add an "Ignore List" with the results being saved on the users browser.
The Ignored List is saved as a JSON array and looks like this:
[{"username":"test_user","date_added":"19/08/13","description":"Don't like this person."},{"username":"test_user_2","date_added":"19/08/13","description":"Don't like this person."}]
And the function required to add the users look like this:
function add_to_ignore_list()
{
var ignored_users = localStorage.getItem("ignore_list"); // returns ignore list
var username = return_current_username(); // returns test_user3
var date = return_current_date(); // returns 19/08/13
var description = prompt("Why do you want to ignore this user?"); // returns desc
add_to_list = {
"username" : username,
"date_added" : date,
"description" : description
};
ignored_users.push(add_to_list);
localStorage["ignore_list"] = JSON.stringify(ignored_users);
$(".user_wrapper").css("background-color","#B40404");
}
For some reason it isn't working and I can't see why Please help.
ignored_users is stored as a string.
When you retrieve it from localStorage, you need to parse it before you use it.
change:
var ignored_users = localStorage.getItem("ignore_list");
to (assumes it had previously been stored):
var ignored_users = JSON.parse(localStorage.getItem("ignore_list"));