简体   繁体   中英

Javascript - Issues with adding to arrays

I am working on a javascript program which builds a powerpoint slide. I am running into issues with dynamically adding elements to an array (which ends up becoming text in a text box). If I explicitly code the text, everything works... This code runs fine...

 slide.addText(
       [  { text: "Bob0", options: {align:'left', color:'757575', font_face: 'Calibri', font_size:9}} ,
          { text: "Bob1", options: {align:'left', color:'757575', font_face: 'Calibri', font_size:9}} ,
          { text: "Bob2", options: {align:'left', color:'757575', font_face: 'Calibri', font_size:9}} ,
          { text: "Bob3", options: {align:'left', color:'757575', font_face: 'Calibri', font_size:9}} ,
          { text: "Bob4", options: {align:'left', color:'757575', font_face: 'Calibri', font_size:9}}               
       ],     
       {shape:pptx.shapes.ROUNDED_RECTANGLE, x:0.33, y:2,w:1.25,h:1.33, line:'757575', line_size:0.5, fill:'FFFFFF',  valign:'top', color:'FFFFFF', font_face:'Calibri Light', font_size:10}
    );  

This code prints 5 lines of text (Bob0...Bob4) inside a rounded Rectangle. What I need to do is dynamically build those lines. There may be just one line, there may be 30, but they all have the exact same format, the only difference being the TEXT attribute is different. So I try to dynamically build the array and access it.

    var arrCRM=[];
..
      for (var i = 0; i < 5; i++) {
           var thisRow = {};  
            thisRow["text"] = "Bob" + i;
            thisRow["options"] = {align:'left', color:'757575', font_face: 'Calibri', font_size:9};
           arrCRM.push(thisRow);
      }; //end of for loop  

This gets me reasonably close. I now need to be able to put arrCRM in the addText routine.

slide.addText(
       [ arrCRM
       ],     
       {shape:pptx.shapes.ROUNDED_RECTANGLE, x:0.33, y:2,w:1.25,h:1.33, line:'757575', line_size:0.5, fill:'FFFFFF',  valign:'top', color:'FFFFFF', font_face:'Calibri Light', font_size:10}
    );  

This gives me a totally unrelated error later in my code, so I know I am doing something really wrong. This however, works...

slide.addText(
       [  arrCRM[0],
          arrCRM[1],
          arrCRM[2],
          arrCRM[3],
          arrCRM[4]
       ],     
       {shape:pptx.shapes.ROUNDED_RECTANGLE, x:0.33, y:2,w:1.25,h:1.33, line:'757575', line_size:0.5, fill:'FFFFFF',  valign:'top', color:'FFFFFF', font_face:'Calibri Light', font_size:10}
    );  

How do I "plug-in" the entire array, since the number of elements in the array will vary...?

You're calling addText([ arrCRM ]) which is adding the value of arrCRM as the single item in a new array, then sending that to addText .

To simplify, this is like sending [[ 1, 2, 3, 4 ]] when the function is expecting [ 1, 2, 3, 4 ] .

What you're probably looking for is to call the function with your array directly:

slide.addText(arrCRM, {
    shape: pptx.shapes.ROUNDED_RECTANGLE,
    x: 0.33,
    y: 2,
    w: 1.25,
    h: 1.33,
    line: '757575',
    line_size: 0.5,
    fill: 'FFFFFF',
    valign: 'top',
    color: 'FFFFFF',
    font_face: 'Calibri Light',
    font_size: 10
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM