jeudi 20 décembre 2018

Conditional statement of cell value in Google App Script

Using this Answer, I have a sidebar with cell contents

https://stackoverflow.com/a/30634581/1045794

Referencing this extract of SidebarJavascript.html

function showRecord(record) {
    if (record.length) {
      for (var i = 0; i < record.length; i++) {
        // build field name on the fly, formatted field-1234
        var str = '' + i;
        var fieldId = 'field-' + ('0000' + str).substring(str.length)

        // If this field # doesn't already exist on the page, create it
        if (!$('#'+fieldId).length) {
          var newField = $($.parseHTML('<div id="'+fieldId+'"></div>'));
          $('#sidebar-record-block').append(newField);
        }

        // Replace content of the field div with new record
        $('#'+fieldId).replaceWith('<div id="'+fieldId+'" class="div-table-row"></div>');
        $('#'+fieldId).append($('<div class="div-table-th">' + record[i].heading + '</div>'))
                      .append('<div class="div-table-td">' + record[i].cellval + '</div>');
      }
    }

I want to display an image if the cell contains .jpg

I have tried

if(record[i].cellval.match(".jpg")!=null){
           // Replace content of the field div with new record
            $('#'+fieldId).replaceWith('<div id="'+fieldId+'" class="div-table-row"></div>');
            $('#'+fieldId).append($('<div class="div-table-th">' + record[i].heading + '</div>'))
                          .append('<div class="div-table-td"><img src="' + record[i].cellval + '"></div>');
} else {
           // Replace content of the field div with new record
            $('#'+fieldId).replaceWith('<div id="'+fieldId+'" class="div-table-row"></div>');
            $('#'+fieldId).append($('<div class="div-table-th">' + record[i].heading + '</div>'))
                          .append('<div class="div-table-td">' + record[i].cellval + '</div>');
}

I think the problem is that I am not correctly setting the conditional statement but I am not sure

Aucun commentaire:

Enregistrer un commentaire