Google Apps Script Listbox Populate based on previous ListBox I am able to get 2 to work but not 3 - javascript

Need a little help figuring out how to get 3 list boxes to populate based on the previous list box. So far what I have is 1 list box that populates from a spreadsheet. Can someone help me with setting up so that the 2nd listbox will populate based off the first and a 3rd listbox that will populate based off of the first listbox? Here is the code:
function doGet(e){
var app = UiApp.createApplication();
var mainPage = app.createVerticalPanel().setId('mainPage');
var dataItemsLB = app.createListBox().setId('dataItemsLB').setName('dataItemsLB');
var dataItemsLbl = app.createLabel('Data Items');
dataItems(dataItemsLB);
mainPage.add(dataItemsLB);
app.add(mainPage);
return app;
}
function dataItems(listbox){
var app = UiApp.getActiveApplication();
var ss = SpreadsheetApp.openById('0AhraBJOts4V3dDhYbERTR0hFNUtNdEhZd2c4OElpY0E');
var list = ss.getSheetByName('dataItems');
var values = list.getRange(1,1,ss.getLastRow(),1).getValues();
for (var i in values){
listbox.addItem(values[i][0].toString());
}
return app;
}
The code above is only for 1 listbox but I thought it would help me to start from the bottom so I understand any explanation.
Thanks for any advice!

Replied on community try this code:
function doGet(e){
var app = UiApp.createApplication();
var mainPage = app.createVerticalPanel().setId('mainPage');
var dataItemsLB = app.createListBox().setId('dataItemsLB').setName('dataItemsLB'); // create a basic list box
var dataItemsLB2 = app.createListBox().setId('dataItemsLB2').setName('dataItemsLB2'); // create a basic list box
var dataItemsLB3 = app.createListBox().setId('dataItemsLB3').setName('dataItemsLB3'); // create a basic list box
var dataItemsLbl = app.createLabel('Data Items');
dataItems(dataItemsLB); // call the "dataItems" function to populate the list box
// Create Server Handlers
var sHandlerLB = app.createServerHandler("listLBSelect");
sHandlerLB.addCallbackElement(mainPage);
dataItemsLB.addChangeHandler(sHandlerLB);
dataItemsLB2.addChangeHandler(sHandlerLB);
mainPage.add(dataItemsLB);
mainPage.add(dataItemsLB2);
mainPage.add(dataItemsLB3);
app.add(mainPage);
return app;
}
function dataItems(listbox){
var app = UiApp.getActiveApplication();
var ss = SpreadsheetApp.openById('0AhraBJOts4V3dDhYbERTR0hFNUtNdEhZd2c4OElpY0E');
var list = ss.getSheetByName('dataItems');
var values = list.getRange(1,1,ss.getLastRow(),1).getValues();
for (var i in values){
listbox.addItem(values[i][0].toString());
}
return app;
}
function listLBSelect(e) {
var app = UiApp.getActiveApplication();
Logger.log(e);
Logger.log("Listbox changed: " + e.parameter.source);
// check which listbox has been changed:
switch(e.parameter.source) {
case "dataItemsLB":
var dataItemsLB2 = app.getElementById("dataItemsLB2");
dataItemsLB2.addItem("aaaaaa");
dataItemsLB2.addItem("bbbbbb");
dataItemsLB2.addItem("cccccc");
break;
case "dataItemsLB2":
var dataItemsLB3 = app.getElementById("dataItemsLB3");
dataItemsLB3.addItem("hhhhhh");
dataItemsLB3.addItem("jjjjjj");
dataItemsLB3.addItem("kkkkkk");
break;
}
return app;
}

Related

If GoogleJsonResponseException: then skip and move to next row

I have a working script. need to improvise to have no manual interruption. We have multiple Profiles in Analytics, sometimes we lose access and sometimes we have. So when i run the Script, If we lost access to 1 of 60 profiles, i have to delete that entry manually then rerun the script.
What i want is, If there is below error, Then skip and continue with next row
"GoogleJsonResponseException: API call to analytics.data.ga.get failed with error: User does not have sufficient permissions for this profile."
function GoogleAnalytics() {
var doc2 = SpreadsheetApp.getActiveSpreadsheet();
var dashboard = doc2.getSheetByName("Dashboard");
for(var i=52;i<65;i++){
var viewId = dashboard.getRange(i,13).getValue(); // Your Google Analytics view ID
var metric = 'ga:metric, ga:metric2, ga:metric3';
var option = {'segment': 'gaid::-5'};
var result = Analytics.Data.Ga.get(viewId, metric, option);
var metric = result.totalsForAllResults['ga:metric'];
var metric2 = result.totalsForAllResults['ga:metric2'];
var metric3 = result.totalsForAllResults['ga:metric3'];
var doc = SpreadsheetApp.getActiveSpreadsheet(); // Current document
var sheet = doc.getActiveSheet(); // Current sheet
sheet.getRange(i,14,1,1).setValue(metric);
sheet.getRange(i,15,1,1).setValue(metric2);
sheet.getRange(i,16,1,1).setValue(metric3);
} }
try it this way:
function GoogleAnalytics() {
var doc2 = SpreadsheetApp.getActiveSpreadsheet();
var sh = doc2.getSheetByName("Dashboard");
var sheet = doc2.getActiveSheet(); // Current sheet
const vs = sh.getRange(52, 13, 13).getValues();
var metric = 'ga:metric, ga:metric2, ga:metric3';
var option = { 'segment': 'gaid::-5' };
for (var i = 0; i < vs.length; i++) {
var viewId = vs[i][0]; // Your Google Analytics view ID
try {
var result = Analytics.Data.Ga.get(viewId, metric, option);
}
catch(e){
continue;
}
if (result) {
sheet.getRange(i + 52, 14, 1, 3).setValues([[result.totalsForAllResults['ga:metric'], result.totalsForAllResults['ga:metric2'], result.totalsForAllResults['ga:metric3']]]);
}
}
}
Without the benefit of working data some of this may not be correct but using setValues and getValues should speed it up considerably and the try catch blocks should help with not getting result consistently. Also you want to avoid making unnecessary declarations in loops.
I might understand the question incorrectly (if so, please clarify) but it sounds to me like you just need to add...
function GoogleAnalytics() {
var doc2 = SpreadsheetApp.getActiveSpreadsheet();
var dashboard = doc2.getSheetByName("Dashboard");
for(var i=52;i<65;i++){
try { //...this line and...
var viewId = dashboard.getRange(i,13).getValue(); // Your Google Analytics view ID
var metric = 'ga:metric, ga:metric2, ga:metric3';
var option = {'segment': 'gaid::-5'};
var result = Analytics.Data.Ga.get(viewId, metric, option);
var metric = result.totalsForAllResults['ga:metric'];
var metric2 = result.totalsForAllResults['ga:metric2'];
var metric3 = result.totalsForAllResults['ga:metric3'];
var doc = SpreadsheetApp.getActiveSpreadsheet(); // Current document
var sheet = doc.getActiveSheet(); // Current sheet
sheet.getRange(i,14,1,1).setValue(metric);
sheet.getRange(i,15,1,1).setValue(metric2);
sheet.getRange(i,16,1,1).setValue(metric3);
} catch(e) { //...this part
console.log(e); //optional, catch(e){} is perfectly valid as well, or any code you might want to execute on error
}
} }

How to Clone an custom entity in dynamics 365 using javaScript

I came across an scenario where I have to clone my custom entity.
Then I have started doing using with JavaScript by placing a custom duplicate button in the ribbon which triggers my JavaScript.
I used the following code:
function duplicateOrder(primaryControl){
var formContext = primaryControl;
var city = formContext.getAttribute('new_city').getValue();
var country = formContext.getAttribute('new_country').getValue();
var state = formContext.getAttribute('new_state').getValue();
var postal_code = formContext.getAttribute('new_zipcodepostalcode').getValue();
// var formItem = formContext.ui.formSelector.items.get();
// alert(`the form item value is ${formItem}`);
formContext.data.entity.save('saveandnew');
sleep(3000);
var city1 = formContext.getAttribute('new_city').getValue();
var country1 = formContext.getAttribute('new_country').getValue();
var state1 = formContext.getAttribute('new_state').getValue();
var postal_code1 = formContext.getAttribute('new_zipcodepostalcode').getValue();
alert(`${city1},${country1},${state1},${postal_code1}`);
if(city1==null){
formContext.getAttribute('new_city').setValue(city);
}
if(country1==null){
formContext.getAttribute('new_country').setValue(country);
}
if(state1==null){
formContext.getAttribute('new_state').setValue(state);
}
if(postal_code1==null){
formContext.getAttribute('new_zipcodepostalcode').setValue(postal_code);
}
alert(`${city},${country},${state},${postal_code}`);
}
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
I tried using the save and new logic to open a new entity form.
The New entity form opens but the field values do not get copied in the new how.
Please help me how to solve this.
Thanks and advance!
You need to call Xrm.Navigation.openForm with your cloneid record.
// Load newly copy record
var entityFormOptions = {};
entityFormOptions["entityName"] = "stdseries";
entityFormOptions["entityId"] = cloneId;
// Open the form.
Xrm.Navigation.openForm(entityFormOptions).then(
function (success) {
formContext.data.refresh();
},
function (error) {
});

Sharepoint 2013 Javascript - Get List Fields

I am on a double learning curve with SharePoint 2013 development and JavaScript.
Please refer to below.
function GetListFields(listname, viewname, ResultCallBackFunction)
{
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle(listname);
if (viewname == "")
{
var view = list.get_views().getByTitle(viewname);
var listFields = view.get_viewFields();
}
else
{
this.listFields = list.get_fields().getByInternalNameOrTitle("Title");
}
clientContext.load(this.listFields);
clientContext.executeQueryAsync(onListFieldsQuerySucceeded, onListFieldsQueryFailed);
function onListFieldsQuerySucceeded()
{
console.log(listFields.get_fields().getByInternalNameOrTitle("Title").get_internalName());
var fldArray = new Array();
var fieldEnumerator = listFields.getEnumerator();
while (fieldEnumerator.moveNext())
{
var oField = fieldEnumerator.get_current();
fldArray.push(oField);
}
ResultCallBackFunction(fldArray);
}
function onListFieldsQueryFailed()
{
alert("Something went wrong. The End is Nigh.");
}
}
The intent is to call GetListFields to return an array with the list's fieldnames. This returns an error "Unable to get property 'get_fields' of undefined or null reference" at the line "console.log....". Please note that this is for my debugging.
If I change the code for function GetListFields as follows:
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle(listname);
if (viewname == "")
viewname = "All Items";
var view = list.get_views().getByTitle(viewname);
this.listFields = view.get_viewFields();
I DO get a result but this contains a field named 'LinkTitle' which I do not have in my list because I have renamed this. What am I missing?
Further to the above, I got an error when using
var listFields = ...
which fixed by doing
this.listFields = ...
but do not understand the difference.
Thanks for any help or pointers.
I took a moment to test your code in my Sharepoint 2013 environment.
I made some changes and now it works.
You can see the resulting script bellow.
Here is some important information about the field names in the result array.
The names of the fields in the view returned by Sharepoint are the internal names and not the title of the field.
This is the reason why you get LinkTitle instead of the real title of your field.
When you create a field in the Sharepoint interface, Sharepoint create a title for the field and also an internal name based on the title.
For example, if I create a field named « my test field », Sharepoint will say the title of the field is « my test field » and the internal name is « my_x0020_test_x0020_field ».
<script>
function GetListFields(listname, viewname, ResultCallBackFunction)
{
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle(listname);
var listFields;
var view;
var defaultViewName = 'All Items';
if (viewname === "")
{
viewname = defaultViewName;
}
view = list.get_views().getByTitle(viewname);
listFields = view.get_viewFields();
clientContext.load(listFields);
clientContext.executeQueryAsync(onListFieldsQuerySucceeded, onListFieldsQueryFailed);
function onListFieldsQuerySucceeded()
{
var fldArray = new Array();
var fieldEnumerator = listFields.getEnumerator();
while (fieldEnumerator.moveNext())
{
var oField = fieldEnumerator.get_current();
fldArray.push(oField);
}
ResultCallBackFunction(fldArray);
}
function onListFieldsQueryFailed()
{
alert("Something went wrong. The End is Nigh.");
}
}
function MyCallBack(fieldArray) {
for (var x=0;x<fieldArray.length;x++) {
console.log(fieldArray[x]);
}
}
</script>
<a id="callGetListFields" href="#" onclick="GetListFields('MyListName','MyViewName', MyCallBack);">Call function GetListFields</a>
Hope this help!

Why won't my Google Calendar Script run?

I am using Google Script to create a form that will create an event, as you can see from the code I have used two panels. I want to create multiple event forms on the 'parent' panel, I am trying to use the 'child' to hold information about the event. I want to run createEvent() on each child panel, but when I run it the script will not run and show this error message:
"Error occured: InternalError: Cannot find method createEvent((class),(class),(class),object)."
function doGet(){
var app = UiApp.createApplication().setTitle('Create Events');
//Create a penel which holds all the form elelemnts
var parent = app.createHorizontalPanel().setId('parent');
var panelOne = app.createVerticalPanel().setBorderWidth(2);
var eventTitleLabel = app.createLabel('Event Title:');
var eventTitle = app.createTextBox();
var eventStartDateLabel = app.createLabel('Event Start Date:');
var evenStartDate = app.createDateBox();
var eventEndDateLabel = app.createLabel('Event End Date:');
var evenEndDate = app.createDateBox();
var eventDeatilLabel = app.createLabel('Event Details:');
var eventDetail = app.createTextArea().setSize('150', '100');
var eventButton = app.createButton('Create Events');
var cancelButton = app.createButton('Cancel');
panelOne.add(eventTitleLabel)
.add(eventTitle)
.add(eventStartDateLabel)
.add(evenStartDate)
.add(eventEndDateLabel)
.add(evenEndDate)
.add(eventDeatilLabel)
.add(eventDetail)
.add(eventButton)
.add(cancelButton);
var eventHandler = app.createServerClickHandler('createEvents');
eventHandler.addCallbackElement(parent);
//Add this handler to the button
eventButton.addClickHandler(eventHandler);
parent.add(panelOne);
app.add(parent);
app.close();
return app;
}
function createEvents(e){
//Get the active application
var app = UiApp.getActiveApplication();
try{
var eventTitle = e.parameter.eventTitle;
var eventStartDate = e.parameter.eventStartDate;
var eventEndDate = e.parameter.eventEndDate;
var eventDetails = e.parameter.eventDetail;
var cal = CalendarApp.getDefaultCalendar();
cal.createEvent(eventTitle, eventStartDate,eventEndDate,{description:eventDetails});
app.add(app.createLabel('Event created Successfully'));
app.getElementById('panel').setVisible(false);
return app;
}
catch(e){
app.add(app.createLabel('Error occured: '+e));
return app;
}
}
All the input widgets need a .setName() to be able to pass their values to the server function. https://developers.google.com/apps-script/guides/ui-service#ServerHandlers
Also consider using Logger.log() for debugging.
You simply forgot to set names to the widgets you want to get data from... in your example :
var eventTitleLabel = app.createLabel('Event Title:');
var eventTitle = app.createTextBox().setName('eventTitle');
var eventStartDateLabel = app.createLabel('Event Start Date:');
var evenStartDate = app.createDateBox().setName('evenStartDate');
var eventEndDateLabel = app.createLabel('Event End Date:');
var evenEndDate = app.createDateBox().setName('evenEndDate');
var eventDeatilLabel = app.createLabel('Event Details:');
var eventDetail = app.createTextArea().setSize('150', '100').setName('eventDetail');

pass argument to mouseCallback

I have a simple, but problematic question.
Is there a way, using google script to send an argument to a mouseCallback?
Some examples are explained here, but each time the cell in written in the function.
I want this cell to change over time, and I need to pass a string to the callback.
This works
function foo() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication();
var button = app.createButton('submit');
app.add(button);
var handler = app.createServerClickHandler('b');
button.addClickHandler(handler);
doc.show(app);
}
function b() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange('a1');
cell.setValue(Number(cell.getValue()) + 1);
var app = UiApp.getActiveApplication();
app.close();
// The following line is REQUIRED for the widget to actually close.
return app;
}
I would like something like this
function foo() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication();
var button = app.createButton('submit');
app.add(button);
var value = 'A1';
var handler = app.createServerClickHandler('b', value);
button.addClickHandler(handler);
doc.show(app);
}
function b(value) {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange(value);
cell.setValue(Number(cell.getValue()) + 1);
var app = UiApp.getActiveApplication();
app.close();
// The following line is REQUIRED for the widget to actually close.
return app;
}
Can anyone help me in this?
Thanks !
There is answers to this question in here :
Thansk to google forum users :)
http://www.google.com/support/forum/p/apps-script/thread?fid=2ef3693fc9aee1050004ad903de88dd4&hl=en

Categories