How I create a SubMenu whit google script - javascript

I need to know how create a simple menu with 2 menu items(A and B): the first one(A) contains 1 item (A1),the second one(B), contains 3 items inside(B1,B2,B3)
I can create an example but always fail:
CODE
var handler = app.createServerHandler();
//Create menu bar
var menuBar = app.createMenuBar(true);
var data = ss.getSheets()[LOG_SHEET_INDEX].getDataRange().getValues();
var lastRow = ss.getLastRow();
for(var row = 1; row < lastRow; row++){
var id_menu =1;
if(data[row][0]==0){
var nivel = data[row][1];
var menuItem = app.createMenuItem(data[row][2], handler);
var separator = app.createMenuItemSeparator();
menuBar.addSeparator(separator);
var subMenu = app.createMenuBar(true).setId("subMenu"+nivel);
for(var i = 1; i< lastRow; i++){
if(data[i][0]==nivel){
var sm = app.getElementById("subMenu"+nivel)
this[sm.addItem(data[row][2], handler)];
}
}menuItem.setSubMenu(sm);
}menuBar.addItem(data[row][2], app.createMenuBar(true));
}
app.add(menuBar);
RESULT
and i need something like (hidding A1,B1,B2,B3 : showing when focus A|B):
____
A
A1
____
B
B1
B2
B3
THIS IS MY SPREAD:
Please Help!!

Hmm, as Serge pointed out my prev. answer is wrong. Meanwhile I found out, what the real reason is:
The MenuItem having the submenu must not have a handler:
var menuBar=app.createMenuBar();
var subb1=app.createMenuBar(true);
subb1.addItem (app.createMenuItem("S1", handler1));
subb1.addItem (app.createMenuItem("S2", handler2));
var mi1=app.createMenuItem("X1", null); // passing null and not a handler makes submenu work...
mi1.setSubMenu(subb1);
menuBar.addItem(mi1);
app.add(menuBar);

I done it but I have a problem with handers which can't be used(both serverhandle and clienthandler) becouse the parameter 'e' is undefined or void: (Also a problem with MenuItemID)
I need to know which button is presed knowing 'e' and dont know whats wrong.
do get(){
var app = UiApp.createApplication()
var menu = menuBar();
var panel = app.createAbsolutePanel().setId("Panel");
var horizontalPanel = app.createHorizontalPanel().setId("horizontalPanel");
var verticalPanelDoc = app.createVerticalPanel().setId("verticalPanelDoc");
var verticalPanelMenu = app.createVerticalPanel().setId("verticalPanelMenu");
var scrollPanelDoc = app.createScrollPanel().setId("scrollPanelDoc").setSize('1000', '600');
verticalPanelMenu.add(menu);
verticalPanelDoc.add(scrollPanelDoc);
horizontalPanel.add(verticalPanelMenu);
horizontalPanel.add(verticalPanelDoc);
panel.add(horizontalPanel)
app.add(panel);
return app;
}
function show(e){
var ss =SpreadsheetApp.openById(TABLA_MENU);
//**testing 'e' = undefined or void**
var e_source = e.parameter.source;
ss.getSheetByName("menu").getRange("B"+6).setValue(e_source);
ss.getSheetByName("menu").getRange("C"+6).setValue(e);
//**result in cells : e_source = undefined e = {parameter={}}**
var app = UiApp.getActiveApplication();
var panel = app.getElementById("Panel");
var verticalPanelDoc = app.getElementById("verticalPanelDoc");
var horizontalPanel = app.getElementById("horizontalPanel");
var scrollPanelDoc = app.getElementById("scrollPanelDoc");
verticalPanelDoc.clear();
verticalPanelDoc.remove(scrollPanelDoc);
//**this will depend of 'e' **
var panelToShow = app.createCaptionPanel().setTitle("Show").setText("Show");
//
var label = app.createLabel().setText("Works!");
panelToShow.add(label);
verticalPanelDoc.add(panelToShow);
return app;
};
function menuBar(){
var ss = SpreadsheetApp.openById(TABLA_MENU);
var app = UiApp.getActiveApplication();
var absolutePanel = app.getElementById("absolutePanel");
var verticalPanelMenu = app.getElementById("verticalPanelMenu");
var horizontalPanel = app.getElementById("horizontalPanel");
var option_menu = app.getElementById("option_menu");
var arrayMenu = ss.getSheetByName("menu").getDataRange().getValues();
var arraySubMenu =ss.getSheetByName("submenu").getDataRange().getValues();
var arrayEnlacesSubMenu =ss.getSheetByName("enlaces").getDataRange().getValues();
var test1 = arraySubMenu[1][1];//A1
var test2 = arrayMenu[1];//A
var test3 = arrayEnlacesSubMenu[1][1]
// **create menu**
var menuBar = app.createMenuBar(true).setAnimationEnabled(true);
var handler = app.createServerHandler("show")
.addCallbackElement(verticalPanelMenu)
.addCallbackElement(horizontalPanel)
.addCallbackElement(absolutePanel);
// **add structure to menu**
for(var k=1; k<arrayMenu.length; k++) {
menuBar.addItem(arrayMenu[k][1], arrayMenu[k][1] = app.createMenuBar(true).setWidth("100%")).setWidth("100%");
for(var j=1; j<arraySubMenu[k].length; j++){
if(arraySubMenu[k][j]!=''){
var enlace = arrayEnlacesSubMenu[j][1].toString();
var a = arrayMenu[k][1];
var b = arraySubMenu[k][j];
//var c = this[arrayMenu[k][1]].addItem(arraySubMenu[k][j],handler).addSeparator(app.createMenuItemSeparator())
arrayMenu[k][1].addItem(arraySubMenu[k][j],handler).setId("1"+arraySubMenu[k][j])).addSeparator(app.createMenuItemSeparator());
//**THIS ID nither set the ID* show in navigator ID = ... class="gwt-MenuItem" id="gwt-uid-18" role="menuitem" aria-hasp....pan="1">Publicaciones 2012</td>
}
}
menuBar.addSeparator(app.createMenuItemSeparator());
}
return menuBar;
};
menuSpreadsheet
submenuSpreadsheet

Sorry, you can't have it (yet): http://code.google.com/p/google-apps-script-issues/issues/detail?id=317

Related

Javascript a TD click and get row cell data

I have this in Jquery all works:
$(document).ready(function() {
$("#checktable td:nth-child(1)").click(function(event){ // This line I need converted
event.preventDefault();
var $td = $(this).closest('tr').children('td'); //This line I need converted
var tid = $td.eq(0).text();
var tdate = $td.eq(1).text();
var tdescribe = $td.eq(2).text();
var wd = $td.eq(3).text();
var dep = $td.eq(4).text();
// ... more code
I need a similar thing in javascript, above only first td is clicked.
My javascript code so far:
function addRowHandlers() {
var table = document.getElementById("checktable2");
var rows = table.getElementsByTagName('tr');
var tid = '';
var tdate = '';
var tdescribe = '';
var wd = '';
var dep = '';
var tisclr = '';
for (var i = 1; i < rows.length; i++) {
rows[i].i = i;
rows[i].onclick = function() {
tid = table.rows[this.i].cells[0].innerText;
tdate = table.rows[this.i].cells[1].innerHTML;
tdescribe = table.rows[this.i].cells[2].innerHTML;
wd = table.rows[this.i].cells[3].innerHTML;
dep = table.rows[this.i].cells[4].innerHTML;
// ... etc more code
The javascript works but any td can be clicked, I am after only:
The first td clicked
Then get parent row
Then all child td's
I have been over dozens of StackOverflow posts and other sites as well... Thanks
And how do I add the event.preventDefault() to regular JS in such a case.
You'd bind the handler to the first .cell.
rows[i].cells[0].onclick = function () {
And then in the handler, access the .parentNode of this to get the row.
And since you're not closing over any variables except those in the function itself (and outside that function, of course), I'd just use a single handler instead of recreating it in the loop.
function addRowHandlers() {
var table = document.getElementById("checktable2");
var rows = table.getElementsByTagName('tr');
var tid = '';
var tdate = '';
var tdescribe = '';
var wd = '';
var dep = '';
var tisclr = '';
for (var i = 1; i < rows.length; i++) {
rows[i].i = i;
rows[i].cells[0].onclick = handler;
}
function handler() {
var row = this.parentNode;
tid = this.innerText;
tdate = row.cells[1].innerHTML;
tdescribe = row.cells[2].innerHTML;
wd = row.cells[3].innerHTML;
dep = row.cells[4].innerHTML;
// etc more code
}
}
I'd probably use a loop to get the desired content too. Maybe like this:
function handler() {
var row = this.parentNode;
var props = ["tid", "tdate", "tdescribe", "wd", "dep"];
var content = props.reduce(function(obj, key, i) {
obj[key] = row.cells[i][i ? "innerHTML" : "innerText"];
return obj;
}, {});
// etc more code
}
Now instead of variables, you have properties of the content object.

I just want to put some texts between tags using appendchild methods with Javascript

This code below is a javascript code to change texts and background properties from the body tag at the same time when the browser is clicked. I've just followed rules in w3school.com, actually, I'm doing the same as the examples do, but mine won't work and I've failed to find my fault. plz, help me.
var helloDiv = document.createElement("div");
var helloText = document.createTextNode("hi.");
helloDiv.appendChild(helloText);
var bodyntext = document.getElementsByTagName("body").appendChild(helloDiv);
var complementary = new Array();
var j = 0;
window.onclick = function(){
var background_color;
var body = document.getElementsByTagName("body")[0];
// var color = new Array();
var result = null;
var number = Math.round(Math.random()*0xFFFFFF);
for(var i = 0; i > 3; i++)
{
complementary[i] = (255-(number.slice(j,j+1).toString(10))).toString(16);
j = j + 2;
}
var clnumber = (complementary[0]+complementary[1]+complementary[2]).toString(16);
body.style.backgroundColor = "#"+ number.toString();
bodyntext.style.color = "#"+ clnumber.toString();
}
Here's your problem
var bodyntext = document.getElementsByTagName("body").appendChild(helloDiv);
Please note that the javascript functions that have plural names often return arrays which is the case here
document.getElementsByTagName("body") returns an array and you are trying to perform an invalid action, that is append child, to this array. You need to access an element using index and then perform this action.
so using
var bodyntext = document.getElementsByTagName("body")[0].appendChild(helloDiv); should fix your problem.
Use document.getElementsByTagName("body")[0] to achieve expected result, as getElementsByTagName() method returns a collection of all elements in the document with the specified tag name, as a NodeList object.
var helloDiv = document.createElement("div");
var helloText = document.createTextNode("hi.");
helloDiv.appendChild(helloText);
var bodyntext = document.getElementsByTagName("body")[0].appendChild(helloDiv);
var complementary = new Array();
var j = 0;
window.onclick = function(){
var background_color;
var body = document.getElementsByTagName("body")[0];
// var color = new Array();
var result = null;
var number = Math.round(Math.random()*0xFFFFFF);
for(var i = 0; i > 3; i++)
{
complementary[i] = (255-(number.slice(j,j+1).toString(10))).toString(16);
j = j + 2;
}
var clnumber = (complementary[0]+complementary[1]+complementary[2]).toString(16);
body.style.backgroundColor = "#"+ number.toString();
bodyntext.style.color = "#"+ clnumber.toString();
}
codepen - https://codepen.io/nagasai/pen/NLBXJE
Please refer this link for more details - https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName
I just change appendChild to document.body.appendChild(helloDiv) and added ID attribute for that div:
var helloDiv = document.createElement("div");
helloDiv.id = "myDIV";
document.body.appendChild(helloDiv);
var helloText = document.createTextNode("Click me");
helloDiv.appendChild(helloText);
var complementary = new Array();
var j = 0;
window.onclick = function(){
var background_color;
var body = document.getElementsByTagName("body")[0];
var helloText = document.getElementById("myDIV");
// var color = new Array();
var result = null;
var number = Math.round(Math.random()*0xFFFFFF);
for(var i = 0; i > 3; i++)
{
complementary[i] = (255-(number.slice(j,j+1).toString(10))).toString(16);
j = j + 2;
}
var clnumber = (complementary[6]+complementary[1]+complementary[2]).toString(16);
var clnumber2 = (complementary[0]+complementary[2]+complementary[6]).toString(11);
body.style.backgroundColor = "#"+ number.toString();
helloText.style.color = "#"+number.toString();
}

Win Pivot App. Need to access some data from a section. Any ideas?

I have a JavaScript Win Pivot Application
Into the Hub I am retrieving some information:
function initPages(options) {
for (var i = 0; i < options.length ; i++) {
var menuItem = options[i];
menuItem.showBanner = (i == 0);
definePages(options);
}
}
and in a .Js file I have the definePages function created:
functions.js:
function definePages(item) {
var action = item[0];
var animation = item[1];
var scyfy = item[2];
var localmovies = item[3];
var clasic = item[4];
var comedy = item[5];
var biography = item[6];
var drama = item[7];
var kids = item[8];
var musical = item[9];
var romantic = item[10];
var suspense = item[11];
var horror = item[12];
var art = item[13];
var personalities = item[14];
var history = item[15];
var society = item[16];
}
Now, in my section 1 I initialize the page by calling another function there:
ready: function (element, options) {
// TODO: Inicializar la página aquí.
options = options || {};
initMovies();
},
function initMovies() {
var element = document.getElementById("movieContainer");
//var movies = ??????????????????????????
//console.log(movies);
//it keeps going
}
I need to be able to retrive, in that var movies, the var action, from the functions.Js or, which is the same, the items[0]...
However, if I call a function in functions.Js, which is defined in section1Page, it won´t work...
I can call functions and pass data from anywhere to functions.Js, but not the other way around...
Any ideas on what should I do? Thanks!!!
I fixed it... I created a global var in function.Js and I get the info from the array in each section later on:
function definePages(item) {
tooSleepyToThink = item;
}
section1Page:
function initMovies() {
var elemento = document.getElementById("movieContainer");
console.log(tooSleepyToThink[0].text);
}

Need some help, callback an array from textarea in googlescript

I need some fresh eyes on what I'm trying to do. I'm working on making our inventory spreadsheet more accurate and email a person when we get low on something. I got most of the code done, there's one part that I'm having issues with and it's where we add a number to certain parts of the spreadsheet. To be more exact, I'm trying to have a menu with multiple text areas for each part, and each text area will relate to the number that we just bought of that part.
My current code for this section has an array that will create and add the textarea to the panel and the part that is labeled in the spreadsheet. A button that will add the text area to the spreadsheet.
My issue that I'm having is correctly setting up the .addCallbackElement() with an array and get whats in the text area to the spreadsheet.
Can anyone see where I am making the mistake, or possible recommendation at something better that I could do?
Thanks for any help that is given
function addBit() {
var ss = SpreadsheetApp.getActive();
var sheet0 = ss.getSheetByName('Inventory');
var lastrow0 = sheet0.getLastRow();
//var ui = SpreadsheetApp.getUi();
//data for each column that we need
var datarange0 = sheet0.getRange('D2:D'+lastrow0);
var datarange1 = sheet0.getRange('E2:E'+lastrow0);
var datarange2 = sheet0.getRange('F2:F'+lastrow0);
var datarange3 = sheet0.getRange('K2:K'+lastrow0);
var datarange4 = sheet0.getRange('I2:I'+lastrow0);
var data0 = datarange0.getValues();// Column D
var data1 = datarange1.getValues();// Column E
var data2 = datarange2.getValues();// Column F
var data3 = datarange3.getValues();// Column K
var data4 = datarange4.getValues();// Column I
var app = UiApp.createApplication();
app.setHeight(500).setWidth(500);
var scroll = app.createScrollPanel().setPixelSize(500,500);
var vpanel0 = app.createVerticalPanel();
var vpanel1 = app.createVerticalPanel();
var hpanel0 = app.createHorizontalPanel();
var apanel = app.createAbsolutePanel();
var text = new Array(lastrow0-1);
//creating labels and text areas for each part for the first column in the menu
for (var i = 0; i <= Math.round((lastrow0 - 2)/2); i++) {
var hpanel = app.createHorizontalPanel();
var label = app.createLabel(data0[i]+ ' ' + data1[i] + 'mm ('+ Math.round(data2[i]) + 'mil)');
text[i] = app.createTextArea().setName('text'+i).setSize(50,20).setValue(0);//.setId('text'+i);
hpanel.add(text[i]);
hpanel.add(label);
vpanel0.add(hpanel);
}
//creating labels and text areas for each part for the second column in the menu
for (var i = Math.round((lastrow0 - 2)/2) + 1; i <= lastrow0 - 2; i++) {
var hpanel = app.createHorizontalPanel();
var label = app.createLabel(data0[i]+ ' ' + data1[i] + 'mm ('+ Math.round(data2[i]) + 'mil)');
text[i] = app.createTextArea().setName('text'+i).setSize(50,20).setValue(0);//.setId('text'+i);
hpanel.add(text[i]);
hpanel.add(label);
vpanel1.add(hpanel);
}
//Creating handlers for the text areas
var thandler = app.createServerHandler('addition');
for (var j = 0; j <= lastrow0 - 2; j++) {
thandler.addCallbackElement(text[j]).setId('text'+i);
}
//when button is hit, the function 'addition' will run
var addButton = app.createButton("Add", thandler);
hpanel0.add(vpanel0);
hpanel0.add(vpanel1);
apanel.add(hpanel0);
apanel.add(addButton);
scroll.add(apanel);
app.add(scroll);
return app;
}
function addition(e){
var app = UiApp.getActiveApplication();
var ss = SpreadsheetApp.getActive();
var sheet0 = ss.getSheetByName('Inventory');
var lastrow0 = sheet0.getLastRow();
//data for each column
var datarange3 = sheet0.getRange('K2:K'+lastrow0);
var datarange4 = sheet0.getRange('I2:I'+lastrow0);
var data3 = datarange3.getValues();// Column K
var data4 = datarange4.getValues();// Column I
var text0 = new Array(lastrow0-1);
//text0[] will have what was in each text area
for (var i = 0; i <= lastrow0 - 2; i++) {
text0[i] = e.parameter.('text' + 1);
}
//for loop will add what was in the text area to columns 'Qty Acq' and 'Current Stock'
for (var j = 0; j <= lastrow0-2; j++) {
var k = j+2;
var v = data3[j] + text0[j];
var x = data4[j] + text0[j]
sheet0.getRange('M' + k).setValue(v);
sheet0.getRange('N' + k).setValue(x);
}
app.close();
}
Use the highest level panel (scrollpanel I guess) as a single callbackElement, it will automatically include all the child widgets.

How to get concotenate value with string to get the value from variable

Please check the script below.
Dynamic form, so the script also dynamic, I have to calculate when the form data changes. during this i am getting some problem.
am getting the value from the variable Final_price1, Final_price2 .....,Final_price7, Final_price8 and then am calculating the total of those.
During this calculation, am concatenating the following concat("Final_price",i); to get the values of the above. This concatenated correctly, but the above variables values are not coming. I dont know why the values are not getting there. So check the script and update me.
function assign_body()
{
var a_7= document.getElementById("option[280]").value;
var spl_7 = a_7.split("_");
//alert(spl);
var cr_7 = spl_7[1];
var operator3_7 = cr_7.split("[");
var symbol7 = operator3_7[0];
var dtt_7 = operator3_7[1];
var myarr_7 = dtt_7.split("$");
var symbol_st_7 = myarr_7[1];
//alert(symbol_st);
//alert(symbol_s);
//var symbol_a = symbol_s.split("(");
//var symbol = symbol_a[1];
//alert(symbol);
var split_value_7 = myarr_7[1];
//alert(split_value);
var final_value_7 =symbol_st_7.split(".");
var Final_price7 =final_value_7[0];
var a_8= document.getElementById("option[281]").value;
var spl_8 = a_8.split("_");
//alert(spl);
var cr_8 = spl_8[1];
var operator3_8 = cr_8.split("[");
var symbol8 = operator3_8[0];
var dtt_8 = operator3_8[1];
var myarr_8 = dtt_8.split("$");
var symbol_st_8 = myarr_8[1];
//alert(symbol_st);
//alert(symbol_s);
//var symbol_a = symbol_s.split("(");
//var symbol = symbol_a[1];
//alert(symbol);
var split_value_8 = myarr_8[1];
//alert(split_value);
var final_value_8 =symbol_st_8.split(".");
var Final_price8 =final_value_8[0];
var j=8;
var total_amount=0;
for(var i=1; i<=j; i++)
{
final_prices=concat("Final_price",i);
alert(final_prices);
symbol_prices=concat("symbol",i);
alert(symbol_prices);
if(isNumber(final_prices)){
alert("number");
/*if(symbol_prices =='+') {
alert("plus");
var total_amount+=parseInt(original_prices)+parseInt(final_prices);
calculated_price_element.innerHTML=total_amount;
alert(total_amount);
} else if(symbol_prices =='-') {
alert("minus");
var total_amount+=parseInt(original_prices)-parseInt(final_prices);
calculated_price_element.innerHTML=total_amount;
alert(total_amount);
}*/
//alert('test');
}
}
}

Categories