How do I retrieve the files from localstorage by the extension name ".log"? - ionic-framework

I am new to angular/js/ionic. I have a really quick question.
How do I retrieve data from localstorage when I don't really know the index name? I just want to grab whatever ends with ".log"?
I understand that the code below will try to retrieve the data by the index 'notes'.
var logFile = angular.fromJson(window.localStorage['notes'] || '[]');

var localLogFile = function() {
for(var i = 0; i < localStorage.length; i++) {
var keyName = localStorage.key(i);
var extension = keyName.substring(keyName.lastIndexOf("."));
if(extension == ".log") {
return localStorage.getItem(keyName);
}
}
};
var logFile = angular.fromJson(localLogFile());

Related

How to loop and collect MultiInput values?

I am using sap.m.MultiInput. How to send that data to the SAP Backend?
I tried using a loop:
for(var i = 0; i < oLenght; i++) {
var oData = this.getView().byId("myMultiInputControl").getTokens()[i].getKey();
}
But oData is holding always a new value. How to hold the data?
you can use a delimiter for example ("/" character) between the keys of the multinput and send the data to the backend System :
if(oMultiInputElement.tokens.length > 1) {
var dataToSend = "";
for(var i = 0; i < oMultiInputElement.tokens.length; i++) {
dataToSend = oFilterData.tokens[i].key + "/" + dataToSend;
}
} else {
dataToSend = oMultiInputElement.tokens[0].key;
}

Clear Data rather then deleting row

I have the script below that works for what i need except the ending where it deletes the row out. What I am in need of is it to copy and paste from Sheet1 to Sheet2, and clear the row out of sheet1 except for column A.
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1'); //source sheet
var testrange = sheet.getRange('D:D');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('Sheet2'); //destination sheet
var data = [];
var j =[];
//Condition check in D:D; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'x') {
data.push.apply(data,sheet.getRange(i+1,1,1,11).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).
setValues(data);
//Delete matched rows in source sheet ** I Need these to clear data
from B:E for row that it copied and pasted rather then deleted it out.***
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k); // I have tried sheet.Clearcontents, with no sucess
//Alter j to account for deleted rows
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
}
I was able to find an alternate way of clearing what i need which was thru another script that clear any orange cell. I used conditional formatting to get this to work. See below. Thanks....
function copyandclearorange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Research'); //source sheet
var testrange = sheet.getRange('L:L');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('Log'); //destination sheet
var data = [];
var j =[];
//Condition check in L:L; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'x') {
data.push.apply(data,sheet.getRange(i+1,1,1,11).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
var sheet = SpreadsheetApp.getActive().getSheetByName('Research');
var range = sheet.getDataRange();
var bgColors = range.getBackgrounds();
for (var i=0; i<bgColors.length; i++) {
for (var j=0; j<bgColors[i].length; j++) {
if (bgColors[i][j] === '#ff9900') {
range.getCell(i+1,j+1).clearContent();
}
}
}
}

how to get 1 item from a data binding in the controller?

I have a table that is filled with data via data binding. And now in my controller i'm trying to loop over all items of the databinding. This is what i have so far but i can't get it to work.
colorRows : function(oTable) {
var items = oTable.getBinding("items");
var rowCount = items.length; //number of visible rows
var currentRowContext;
for (var i = 0; i < rowCount; i++) {
currentRowContext = items[i].getValue(); //this won't work
}
}
so i need to get a value from the item with the index that matches i.
edit: i'm using sap.m.table
As Keshet mentioned, it depends on the Table you are using. Here is an exapmle for the sap.ui.table.Table. First you get the Context of each Row and then you can access the Data that is saved on the Row (btw, there is no such thing as RowValue):
colorRows: function(oTable) {
var aRows = oTable.getRows();
var currentRowValue;
for (var i = 0; i < aRows.length; i++) {
var oRowContext = oTable.getContextByIndex(i);
if (oRowContext) {
var oRowObject = oRowContext.getObject();
// or you can use the getProperty method
var oSingleValue = oRowContext.getProperty("yourPropertyName");
}
}
}
I assume you use sap.m.Table.
colorRows : function(oTable) {
var sPath = oTable.getBinding("items").getPath(); //path to table's data
var oModel = this.getView().getModel(); //model which is bound to the table
//or var oModel = oTable.getModel(); if the model is bound directly to the table
var aData = oModel.getProperty(sPath);//array of rows
var rowCount = aData.length;
var currentRowContext;
for (var i = 0; i < rowCount; i++) {
currentRowContext = aData[i];
}
}
Here is a working example.

Google Spreadsheet - How to avoid sending email duplicates?

I am having an issue with a script. I used the following script from Google Developers Website in order to do a simple merge mail. See https://developers.google.com/apps-script/articles/mail_merge
I modified a bit the script so to prevent email duplicates. However, even if the script seems to work as it marks 'EMAIL_SENT' in each row every time an email is sent. It does not pay attention if the mail as already been marked and still send the mail.
I believe there is an error at line 16 "var emailSent = rowData[6];"
I would really appreciate if someone could help me. Whoever you are thanks in advance.
Here is the modified script :
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 7);
var templateSheet = ss.getSheets()[1];
var emailTemplate = templateSheet.getRange("A2").getValue();
var objects = getRowsData(dataSheet, dataRange);
for (var i = 0; i < objects.length; ++i) {
var Resume = DriveApp.getFilesByName('Resume.pdf') var Portfolio = DriveApp.getFilesByName('Portfolio.pdf') var rowData = objects[i];
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
var emailSubject = "Architectural Internship";
var emailSent = rowData[6];
if (emailSent != EMAIL_SENT) {
MailApp.sendEmail(rowData.emailAddress, emailSubject, emailText, {
attachments: [Resume.next(), Portfolio.next()]
});
dataSheet.getRange(2 + i, 7).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
function fillInTemplateFromObject(template, data) {
var email = template;
var templateVars = template.match(/\${\"[^\"]+\"}/g);
for (var i = 0; i < templateVars.length; ++i) {
var variableData = data[normalizeHeader(templateVars[i])];
email = email.replace(templateVars[i], variableData || "");
}
return email;
}
function getRowsData(sheet, range, columnHeadersRowIndex) {
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.getValues(), normalizeHeaders(headers));
}
function getObjects(data, keys) {
var objects = [];
for (var i = 0; i < data.length; ++i) {
var object = {};
var hasData = false;
for (var j = 0; j < data[i].length; ++j) {
var cellData = data[i][j];
if (isCellEmpty(cellData)) {
continue;
}
object[keys[j]] = cellData;
hasData = true;
}
if (hasData) {
objects.push(object);
}
}
return objects;
}
function normalizeHeaders(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeader(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
return keys;
}
function normalizeHeader(header) {
var key = "";
var upperCase = false;
for (var i = 0; i < header.length; ++i) {
var letter = header[i];
if (letter == " " && key.length > 0) {
upperCase = true;
continue;
}
if (!isAlnum(letter)) {
continue;
}
if (key.length == 0 && isDigit(letter)) {
continue;
}
if (upperCase) {
upperCase = false;
key += letter.toUpperCase();
} else {
key += letter.toLowerCase();
}
}
return key;
}
// Returns true if the cell where cellData was read from is empty. // Arguments: // - cellData: string function isCellEmpty(cellData) {
return typeof(cellData) == "string" && cellData == "";
}
// Returns true if the character char is alphabetical, false otherwise. function isAlnum(char) { return char >= 'A' && char <= 'Z' || char >= 'a' && char <= 'z' || isDigit(char); }
// Returns true if the character char is a digit, false otherwise. function isDigit(char) { return char >= '0' && char <= '9'; }
Your code is really hard to read and the functions that return 2 or more objects make it even harder...you are using variable names that are also a bit confusing.... but that is probably a personal pov :-)
Anyway, I think I've found the issue: when you write var rowData = objects[i];
This "object" is actually the result of the getRowData function but if you look at this function, you'll see that it returns 2 objects, the first one being itself the result of another function (getObjects) ...
You are checking the value is the 6th element of the array which is actually an object and compare it to a string. The equality will never be true.
I didn't go further in the analyse since I found it really confusing ( as I already said) but at least you have a first element to check .
I would suggest you rewrite this code in a more simple way and use more appropriate variable names to help you while debugging.
I would recommend logging both values before executing to make sure they are the same. I would also guess that the email_sent and EMAIL_SENT are different data types. Can also try forcing the value to string for comparison.
To clarify:
logger.Log(emailSent);
logger.Log(EMAIL_SENT);
if (emailSent.toString() != EMAIL_SENT.toString())
{...
Error is in this line of code -
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
It's considering only 2 columns in the range. Changed 2 to 3 and it worked fine.

get values from XML file in my Titanium Project

I am having the following .xml which i used in my android project.now,am trying to create the same in iPhone using titanium.Can some one tell me how to get this values from the xml file and show them in a table view.? if i give MainCategories the 3 values in it should appear in the table view.Also can some one tell me is there any other better option to achieve this?or can we use plist??thank you.
<string-array name="MainCategories">
<item>Acceleration</item>
<item>Angle</item>
<item>Area</item>
</string-array>
<string-array name="Acceleration_array">
<item>meter/sq sec</item>
<item>km/sq sec</item>
<item>mile/sq sec</item>
<item>yard/sq sec</item>
</string-array>
<string-array name="Angle_array">
<item>degree</item>
<item>radian</item>
<item>grad</item>
<item>gon</item>
</string-array>
Try This.
var result = [];
var fileName = 'arrays1.xml'; //save xml file
var tableView = Ti.UI.createTableView({ data : result, width : '100%', height : '100%' });
function readXML(fileName)
{
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, fileName);
var xmltext = file.read().text;
var doc = Ti.XML.parseString(xmltext);
var parentNodeLength = doc.documentElement.getElementsByTagName('string-array').length;
for (var i = 0; i < parentNodeLength; i++) {
var attrValue = doc.documentElement.getElementsByTagName('string-array').item(i).attributes.getNamedItem('name').nodeValue;
if (attrValue === 'Angle_array') {
var parentNode = doc.documentElement.getElementsByTagName('string-array').item(i);
var subNodeLength = parentNode.getElementsByTagName('item').length;
for (var j = 0; j < subNodeLength; j++) {
var title = parentNode.getElementsByTagName('item').item(j).text;
var row = Ti.UI.createTableViewRow({
height : 110
});
var label = Ti.UI.createLabel({
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
text : title
});
row.add(label);
result.push(row);
}
}
}
}
readXML(fileName);
tableView.setData(result);
win1.add(tableView);
win1.open();
i am not familiar with titanium but maybe this should help you out:
var result = this.responseText;
var xml = Ti.XML.parseString(result);
var params = xml.documentElement.getElementsByTagName("member");
var name = xml.documentElement.getElementsByTagName("name");
var value = xml.documentElement.getElementsByTagName("string");
var data = [];
for (var i=0;i<params.item.length;i++) {
Ti.API.log('Param '+i+': Name: '+n.item(i).text);
Ti.API.log('Param '+i+': Value: '+v.item(i).text);
// Add to array
data.push({"name":n.item(i).text,"value":v.item(i).text});
}
copied from this link
for displaying it in uitableview, save your values in nsarray and display your array in tableview. here is a tutorial for UITableView.