unable to set and get values in localstorage - ios

I'm developing a web application in iPhone using Phonegap. In this app, I want to send data from one page to another within the same index.html file. I'm using local storage to send data, but I am unable to send values. whenever the local storage statements are encountered, it skips the remaining statements also in that javascript function. Please tell me the solution. Do I need to add any plugin to use local storage?
{
var name = document.getElementById("usernameTextField").value; //TextField of page1
var fullname = document.getElementById("nameLabel").innerHTML; //label of page1
localstorage.setItem("userName", name);
localstorage.setItem("fullname1", fullname);
var getusername = localstorage.getItem("userName");
var getname = localstorage.getItem("fullname1");
fullnameLabel.innerHTML = getname.value; //label of page2
userNameLabel.innerHTML = getusername.value; //label of page2
}

Try with this
{
var name = document.getElementById("usernameTextField").value; //TextField
var fullname = document.getElementById("nameLabel").innerHTML; //label
localStorage.setItem("userName", name);
localStorage.setItem("fullname1", fullname);
var getusername = localStorage.getItem("userName");
var getname = localStorage.getItem("fullname1");
alert(getname + " " + getusername); //label
}
Please check the full code here

Related

Sharing same google form across multiple spreadsheets

I have created a Feedback form and want to open it from and share it across multiple spreadsheets. I have used a revised version of the code in the following post Single Google Form for multiple Sheets.
The form opens as expected as an IFRAME but does not allow me (or any user) to populate the fields and create a response. What am I missing?
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Data Architecture')
.addItem('Create File Note', 'menuItem1')
.addSeparator()
.addSubMenu(ui.createMenu('Feedback & Actions')
.addItem('Provide Feedback', 'provideFeedback')
.addItem('Create and Action', 'createAction'))
.addToUi();
}
function provideFeedback() {
var documentName = SpreadsheetApp.getActive().getName();
var dataentityName = SpreadsheetApp.getActiveRange().getCell(1, 1).getDisplayValue();
launchForm();
}
function createAction() {
var documentName = SpreadsheetApp.getActive().getName()
var documentID = SpreadsheetApp.getActive().getId()
var dataentityName = SpreadsheetApp.getActiveRange().getCell(1, 1).getDisplayValue()
launchForm();
}
function launchForm(){
var formID='1pmH3AWiMUczat5uIaZ5zaT--cmDjq9v3W9pePPjwGF0';
var form = FormApp.openById(formID);
var formURL = form.getPublishedUrl();
var response = UrlFetchApp.fetch(formURL);
var formHtml = response.getContentText();
var htmlApp = HtmlService
.createHtmlOutput(formHtml)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Feedback and action form')
.setWidth(800)
.setHeight(800);
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}
Instead of accessing the web page and generating the HTML content and serve it again, you can use iframe tag with src to your URL to do it for you.
function launchForm(){
var formID='1pmH3AWiMUczat5uIaZ5zaT--cmDjq9v3W9pePPjwGF0';
var form = FormApp.openById(formID);
var formURL = form.getPublishedUrl()+ "?embedded=true"
var htmlApp = HtmlService
.createHtmlOutput('<iframe src="'+formURL+'" width="500" height="400" frameborder="0" marginheight="0" marginwidth="0"> </iframe>')
.setTitle('Feedback and action form')
.setWidth(400)
.setHeight(400);
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}
To have pre-filled form to be used in your script:
1) Get a prefilled Form, by following the steps here
2) For a form like this:
the prefilled url will look like this:
var formURL = "https://docs.google.com/forms/d/e/{Publish ID}/viewform?
usp=pp_url&
entry.1739076204=Jack+Brown&
entry.1869878870=2017-05-12&
entry.799903038=Incorrect&
entry.1996064114=I+am+the+best&
entry.607023772=Select+Me"
I have separated out the individual options so that it is easier to compare with an entry in the form. Now to modify the entry all you have to do is modify the value after '=' sign for each entry. So to change the name you would do:
var name = "Chuck Norris"
entry.1739076204='+name+'
Similarly, you can modify each enter by modifying the URL. The below code modifies the entry for a specific form I have hosted. You will have to modify if as per your needs
function launchForm(){
var publishedURl = "https://docs.google.com/forms/d/e/1FAIpQLSdsHM53jTWJ0Eqn8VSxE5bWHjnD9KXsVBrqLsBwtyJIIsjpnA/viewform"
var name = "Chuck Not Norris"
var dt = Utilities.formatDate(new Date(), "GMT-05:00", "yyyy-MM-dd")
var options1 = ["Correct","Incorrect"]
var options2 = "Chuck Norris is google Apps Scripts"
var options3 = ["Select Me","Don't select me"]
var preEditUrl = publishedURl + '?usp=pp_url&entry.1739076204='+name
+'&entry.1869878870='+dt
+'&entry.799903038='+options1[0]
+'&entry.1996064114='+options2
+'&entry.607023772='+options3[0]
var htmlApp = HtmlService
.createHtmlOutput('<iframe src="'+preEditUrl+'" width="500" height="400" frameborder="0" marginheight="0" marginwidth="0"> </iframe>')
.setTitle('Feedback and action form')
.setWidth(400)
.setHeight(400);
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}
Hopefully, this will get you started in the right direction.

Creating a hyperlink in active cell from uploaded file

I'm designing a shared Google Sheets for our team to keep track of each piece of content we produce. I want to implement a feature that allows people to upload a preview clip and have a hyperlink automatically created within the active cell.
My script so far serves up HTML as a user interface with a file upload and name entry. This part works fine and allows anyone to upload straight to Google Drive.
I've been having trouble getting it to automatically create a hyperlink in the active cell to the uploaded file. Been searching around, but haven't had a great deal of luck.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('File Upload')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('form.html')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi()
.showModalDialog(html, 'Upload A File');
}
function uploadFiles(form) {
try {
var dropbox = "Clips";
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var blob = form.myFile;
var file = folder.createFile(blob);
file.setDescription("Uploaded by " + form.myName);
return "File uploaded successfully " + file.getUrl();
} catch (error) {
return error.toString();
}
}
My suggestion is to modify the return value from uploadFiles() to be an object, then use the URL to populate a spreadsheet HYPERLINK() formula.
return "File uploaded successfully " + file.getUrl();
Becomes:
return {
result: "File uploaded successfully",
fileURL: file.getUrl(),
fileDesc: file.getDescription() // Could be other values
};
Next, a function that sets the formula. This server-side function would be called with the values to be used in the formula, which were previously returned from uploadFiles(). I'm assuming this is from your client-side JavaScript, but that's just a guess, since you didn't include that in your question.
function setHyperlink( fileURL, fileDesc ) {
var formula = '=HYPERLINK("' + fileURL + '","' + fileDesc + '")';
SpreadsheetApp.getActiveCell()
.setFormula( formula );
return true;
}
I ended up solving this issue using the GAS Properties Service - creating 2 new User Properties to contain URL and Name data.
I also found a few issues with getActiveCell - it kept placing the link in A1. Although I had used Google's suggested method for returning the active cell, I was able to use the fix suggested here:
http://bit.ly/20Gc7l6
Here's my final script
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('File Upload')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('form.html')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi()
.showModalDialog(html, 'Upload A File');
}
function uploadFiles(form) {
try {
var dropbox = "Blacksand Clips";
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var blob = form.myFile;
var file = folder.createFile(blob);
file.setDescription(file.getDescription());
var userProperties = PropertiesService.getUserProperties();
var link = file.getUrl();
var name = file.getName();
userProperties.setProperty('link', link);
userProperties.setProperty('name', name);
setHyperlink();
return "File uploaded successfully ";
} catch (error) {
return error.toString();
}
}
function setHyperlink() {
var userProperties = PropertiesService.getUserProperties();
var link = userProperties.getProperty('link');
var displayName = userProperties.getProperty('name');
var value = 'hyperlink("' + link + '","' + displayName + '")'
var ss = SpreadsheetApp.getActiveSheet();
var cell = ss.getActiveCell().activate();
cell.setFormula( value )
return true;
}

IOS automation script error

What wrong with this automation script.I have only one textfield where user enters his email id.Everytime i run this,i keep getting "Wrong number of text fields".
var target = UIATarget.localTarget();
var app = target.frontMostApp();
var window = app.mainWindow();
target.logElementTree();
//var textfieldscount = UIATarget.localTarget().frontMostApp().mainWindow().textFields()["UserText"];
var textfieldscount = UIATarget.localTarget().frontMostApp().mainWindow().textFields()[0];
var count = textfieldscount.length;
//var count = view.textFields();
if(count!=1){
UIALogger.logFail("Wrong number of text fields");
}else{
UIALogger.logFail("Right number of text fields");
}
In case of a single Test field on your app window it's better to handle it using the code below (if you simply want to check whether it's visible and has got a correct name):
var txt_field_name = textfieldscount .name();
if (textfieldscount .isVisible () && txt_field_name .match("UserText")){
UIALogger.logPass("Txt field is on a screen");
... YOUR_CODE_HERE...
} else {
UIALogger.logFail("Wrong number of text fields");
}

Get text from textarea

I feel like I am going insane trying to get the most simple of things to work! I have a mailing list in Google Spreadsheet on Google Drive. I have created a user interface that prompts for the message that the user wants to include in the email. My code looks like this.
function startBulkEmailUI(e) {
//create main form
var app = UiApp.createApplication().setTitle('Send Email').setWidth(602).setHeight(402);
var scrollPanel = app.createScrollPanel().setId('scrollPanel').setAlwaysShowScrollBars(true) ;
var mainPanel = app.createVerticalPanel().setId('mainPanel');
mainPanel.setStyleAttribute('border', '1px solid #C0C0C0').setWidth("100%").setHeight("100%");
scrollPanel.add(mainPanel);
//create panel for input boxes etc
var inputPanel = app.createVerticalPanel().setId('inputPanel').setWidth("100%").setHeight("100%").setSpacing(0);
var messageLabel = app.createLabel('E-mail Message');
var messageBox = app.createTextArea().setName('messageBox').setId('messageBox').setVisible(true).setWidth(400).setHeight(150);
var attachmentLabel = app.createLabel('E-mail Attachment');
var attachmentCheckBox = app.createCheckBox('Attach a file').setValue(false);
inputPanel.add(messageLabel).add(messageBox).add(attachmentLabel).add(attachmentCheckBox);
mainPanel.add(inputPanel).setCellHorizontalAlignment(inputPanel, UiApp.HorizontalAlignment.CENTER) ;
inputPanel.setCellHorizontalAlignment(messageLabel, UiApp.HorizontalAlignment.CENTER);
inputPanel.setCellHorizontalAlignment(messageBox, UiApp.HorizontalAlignment.CENTER);
inputPanel.setCellHorizontalAlignment(attachmentLabel, UiApp.HorizontalAlignment.CENTER);
inputPanel.setCellHorizontalAlignment(attachmentCheckBox, UiApp.HorizontalAlignment.CENTER);
//add function buttons
var buttonsPanel = app.createHorizontalPanel().setStyleAttribute('margin', '20px').setWidth(500);
var closeButton = app.createButton('Close',app.createServerHandler('close_'));
var sendButton = app.createButton('Send!').setId("sendButton");
buttonsPanel.setId('buttonsPanel').add(closeButton).add(sendButton);
mainPanel.add(buttonsPanel).setCellHorizontalAlignment(buttonsPanel, UiApp.HorizontalAlignment.CENTER);
buttonsPanel.setCellHorizontalAlignment(closeButton, UiApp.HorizontalAlignment.CENTER).setCellHorizontalAlignment(sendButton, UiApp.HorizontalAlignment.CENTER);
app.add(scrollPanel);
//handlers
var handlerSendEmails = app.createServerClickHandler('sendEmails');
handlerSendEmails.addCallbackElement(inputPanel);
sendButton.addClickHandler(handlerSendEmails);
//show the form
ss.show(app);
return app;
}
function sendEmails(e) {
var contactDetail = getContactDetail();
var count = 0;
var failed = 0;
for (i in contactDetail) {
var row = contactDetail[i];
var emailAddress = row[9];
var stuff = e.parameter.messageBox.value;
Logger.log(e.parameter);
var message = 'Hi, ' + row[4] + '\n\n' + 'This is a test!'; // Second column
var subject = 'Test Email';
try {
// MailApp.sendEmail(emailAddress, subject, message);
count = count+1;
} catch(e) {
failed = failed+1;
}
}
var msg = stuff;
Browser.msgBox('Debug Msg', msg, Browser.Buttons.OK);
}
I want the user to enter information into the textarea, which I can then use as the body of an email. I discovered there is no getText, so I have found hundreds of answer saying it should look like the code above: you create a callbackhandler, then use e.parameter.OBJECT_NAME.value to get the text. It simply does not work for me and I have tried every workaround that I can think of. Any suggestions? Is my code faulty (I know it's messy...)?
The "normal" way to get a widget's value in a handler function is in the form e.parameter.widgetName so in you case you should simply use
var stuff = e.parameter.messageBox; without the ".value" , the result is a string.

How do you get the selected listBox item as a String?

I have been trying to build a small app that takes the selected item from a listBox and appends it to the filename of a file that will be uploaded. I can display everything correctly, and the file gets uploaded, but the value that should be appended to the filename is 'undefined'. Here is a sample:
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload CSV to Sheet");
var panel = app.createVerticalPanel();
var listBox = app.createListBox().setName('myList').setId('myList').setWidth('80px');
listBox.addItem('Value1');
listBox.addItem('Value2');
listBox.addItem('Value3');
listBox.addItem('Value4');
panel.add(listBox);
app.add(panel);
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton('Submit'));
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
return app;
}
function doPost(e) {
var fileBlob = e.parameter.thefile;
var app = UiApp.getActiveApplication();
var doc = DocsList.createFile(fileBlob).rename(e.parameter.myList+Utilities.formatDate(new Date(), "GMT", "MM-dd-yy"));
return app;
}
How can I get the name of the file to take the value from the listBox?
You added the listBox to the app instead of adding it to the formpanel content.
This has 2 consequences :
1 - the list stays visible after submitting
2 - the list is not received by the doPost since it is not in the form...
Try it like this :
function doGet() {
var app = UiApp.createApplication().setTitle("Upload CSV to Sheet");
var panel = app.createVerticalPanel();
var listBox = app.createListBox().setName('myList').setId('myList').setWidth('80px');
listBox.addItem('Value1');
listBox.addItem('Value2');
listBox.addItem('Value3');
listBox.addItem('Value4');
panel.add(listBox);
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton('Submit'));
formContent.add(panel);
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
return app;
}
function doPost(e) {
var fileBlob = e.parameter.thefile;
var doc = DocsList.createFile(fileBlob).rename(e.parameter.myList+' '+Utilities.formatDate(new Date(), "GMT", "MM-dd-yy"));
var app = UiApp.getActiveApplication();
return app;
}

Resources