0

I'm creating a script that writes data onto a Gadget using createHTML but for some reason nothing is written to the gadget when I use a variable, directly inserting text works perfectly i.e. createHTML('Hi').

Here is my code:

function doGet() {
var app = UiApp.createApplication();
var spreadsheet = SpreadsheetApp.openById('');
var sheet = spreadsheet.getSheetByName('Users');
var user = Session.getActiveUser();
var site = SitesApp.getActivePage();
var url = site.getUrl();
var data = sheet.getDataRange().getValues();

Logger.log(url);
Logger.log(user);

for (var i = 0; i < data.length; i++) {
  var row = data[i];
  var urlI = row[13];
  if (urlI == url){
    var about = row[12]
    Logger.log(about);
    if (about = "")
    {
    Logger.log('0');
    }
    else
    {
      var aboutTxt = app.createHTML('<p>' + about + '</p>');
      app.add(aboutTxt);
      break;
    }
  }
}
 return app;
}

I think that when reading the value from the spreadsheet into the variable the data type is not string but object or something else but I'm unsure. If this is the case does anyone know how to convert the data type to string or another workaround?

Thank you in advance for your help.

8
  • I see you use the Logger quite a lot (good idea! -) but why didn't you put a logger right where you need it near the setHTML call? It could answer your question ... Jul 11, 2012 at 15:46
  • Hi Serge. I did but in order to determine what the value inside the about variable is. According to the log everything is fine but once I try to pass the value to a label or the createHTML method nothing shows up on the gadget. Jul 11, 2012 at 21:23
  • There are some restrictions on HTML tags... what is the content of 'about'? Jul 11, 2012 at 21:30
  • Well it is quite a long piece of text but I've tried another cell which is only one word and that didn't work either. I also tried inserting the string directly i.e createHTML('The text from the cell') and that works perfectly. Jul 11, 2012 at 21:45
  • see answer please to test if this works with you, it does here. Jul 11, 2012 at 21:57

2 Answers 2

1

Your code looks ok. Are you sure that the execution is getting into the else{} part ? You mentioned that the data type might not be a String. To convert something to a String you can use the toString() method.

For example:

about.toString() 
1
  • Hi, thanks for your reply. I tried converting the variable value but it seems like the compiler is ignoring the conversion function. For example if I use about2 = about.toString() and then Logger.log(about2) nothing gets written to the log. Jul 11, 2012 at 21:25
0

Can you try this simple test , just fill a couple of cells on a row of cells with some html content , the UI will update when button is pressed.

var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
//
function move() {
   var app = UiApp.createApplication().setTitle("move test")
       .setHeight(100).setWidth(400).setStyleAttribute("background-color","beige");
   var panel = app.createVerticalPanel();
   var next = app.createButton('next').setWidth('180');
   var chkmode = app.createCheckBox("moving mode (checked = up/dwn, unchecked=L/R)").setValue(false).setName('chkmode');
   var cellvaluestring = sh.getActiveRange().getValue().toString();
   var label = app.createHTML('<p>' + cellvaluestring + '</p>').setId('label');
   panel.add(next).add(chkmode).add(label);
   var handler = app.createServerHandler('click').addCallbackElement(panel);
   next.addClickHandler(handler);
   app.add(panel);
   ss.show(app);
 }
//
function click(e) {
  var app = UiApp.getActiveApplication();
  var activeline = sh.getActiveRange().getRow();// get the row number of the selected cell/range
  var activecol = sh.getActiveRange().getColumn();// get the row number of the selected cell/range
  var cellvaluestring = sh.getActiveRange().getValue().toString();
  var label = app.getElementById('label');
  label.setHTML('</p>'+cellvaluestring+'</p>');
  var chkmode=e.parameter.chkmode;
  if(chkmode=="true"){
    activeline++
    }else{
      activecol++}
  var sel=sh.getRange(activeline,activecol);
  sh.setActiveSelection(sel);// make the next row active
  return app;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.