Search code examples
javascriptgoogle-apps-scriptgmail-addons

Navigating between two cards


I am making a gmail add-on where I am trying to navigate between two cards in gmail ad don using google App script. But i am getting a error.

Error with the add-on. Run time error. Object does not have property - /​Card/​header. [line: 42, function: notifyUser, file: Code]

This is my manifest code

{
  "oauthScopes": [
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/gmail.readonly",
    "https://www.googleapis.com/auth/script.storage",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/spreadsheets"    
  ],
   "urlFetchWhitelist": [
      "https://mail.google.com/",
      "https://sites.google.com/fcpl.biz/fthub/ft-hub/"
    ],
  "gmail": {
    "name": "Finetech",
    "logoUrl": "https://sites.google.com/a/oursrilanka.com/testing-url/gmail-addons/sound.png",
    "contextualTriggers": [{
      "unconditional": {},
      "onTriggerFunction": "buildCard"

    }],
    "openLinkUrlPrefixes": [
      "https://mail.google.com/",
      "https://sites.google.com/fcpl.biz/fthub/ft-hub/"
    ],
    "primaryColor": "#4285F4",
    "secondaryColor": "#4285F4",
    "version": "TRUSTED_TESTER_V2"
  }
}

This is my code.gs file

    //Spread Sheet ID
var SS_ID = "181tnith14lu8ttAvtqsU3gHi32-UjcrPqH5Pjuenk5A";

function buildCard(){
    var list=getPost();
    Logger.log(list);
    var card = CardService.newCardBuilder();
    card.setHeader(CardService.newCardHeader().setTitle("<font color=\"#0117a1\"><b>FT News Feed</b></font>"));
    var section = CardService.newCardSection().setHeader("<font color=\"#1257e0\">Announcements</font>");
    for(var i=0; i<list.length ;i++){
      section.addWidget(CardService.newTextParagraph().setText(list[i].para.slice(0,85)+"..."+"<font color=\"#FF0000\">view</font>")); 
    }

    var button = CardService.newTextButton().setText('More Detail').setOpenLink(CardService.newOpenLink().setUrl("https://sites.google.com/fcpl.biz/fthub/ft-hub"));
    section.addWidget(CardService.newButtonSet().addButton(button));
    card.addSection(section);

   var section2 = CardService.newCardSection().setHeader("<font color=\"#1257e0\">OverC </font>");
   section2.addWidget(CardService.newTextParagraph().setText(''));
   card.addSection(section2);

   var section3 = CardService.newCardSection().setHeader("<font color=\"#1257e0\">Finetech Apps</font>");
   section3.addWidget(CardService.newTextParagraph().setText(' '));
      var buttonAction = CardService.newAction()
        .setFunctionName('notifyUser');
  section3.addWidget(CardService.newTextButton()
        .setText('Notify')
        .setOnClickAction(buttonAction));
  card.addSection(section3);

  return card.build();
 }

  function notifyUser() {
   var card = CardService.newCardBuilder();
   var section2 = CardService.newCardSection().setHeader("<font color=\"#1257e0\">OverC </font>");
   section2.addWidget(CardService.newTextParagraph().setText(''));
   card.addSection(section2);

    return card.build();
  }

///////////////////////////////////////////////////////////////////////////////

var ss = SpreadsheetApp.openById(SS_ID);
var sheet = ss.getSheets()[0];

function getPost(){

    var todayDate = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy");
    var lastRaw = sheet.getLastRow();
    var list = [];

    //get data from sheet
    for(var i=lastRaw; i > 1; i--){ 
        if(getClosingDate(i) > todayDate){
              var obj = {
                "para": getPara(i),
                "time":getTime(i),
              };
          list.push(obj);
          }          
    }

    return list;
}

function getPara(raw){
  var para = sheet.getRange(raw,3.0).getValue();  
  return para;    
}

function getClosingDate(raw){
     var dueDate = new Date(sheet.getRange(raw,5.0).getValue());
     //Logger.log("Due: "+dueDate);
     //var nRow = row+1;
     dueDate.setDate(dueDate.getDate()+1);
     var closingDate=Utilities.formatDate(dueDate, "GMT", "MM/dd/yyyy");
     return closingDate;   
}

function getTime(raw){
     var time = sheet.getRange(raw, 1.0).getValue().toString();
     var editTime=time.slice(0,24);  
     return editTime;
}

Solution

  • You need to add a CardHeader to the Card created inside the notifyUser method.

      function notifyUser() {
        var card = CardService.newCardBuilder();
        card.setHeader(CardService.newCardHeader().setTitle("Notify User"));
        var section2 = CardService.newCardSection().setHeader("<font color=\"#1257e0\">OverC </font>");
        section2.addWidget(CardService.newTextParagraph().setText(''));
        card.addSection(section2);
        return card.build();
      }