Google apps脚本:将inlineImage和text插入Table Cell并修改单元格中的文本

时间:2014-11-19 03:56:12

标签: google-apps-script google-sheets google-docs tabular inline-images

我要做的是使用Google应用脚本在Google文档中创建一个表格,以及(1)将inlineImage插入单元格(同时将图像大小限制为大约120像素宽)。然后我想(2)将文本插入图像下方的同一单元格中并修改文本的字体大小。

我创建了一个能够在段落中完成所有这些操作的脚本,以便插入一个带有文本的图像。但是,我需要能够并排放置多张图片。由于我不认为Google Docs支持多列,我被告知我可以使用带有不可见边框的表(可能有3列)来获得类似的效果。如果有任何建议如何以不同的方式或任何问题,请告诉我。

我的脚本从Google表格中获取信息和图片链接,并使用它们在Google文档中创建照片目录。我试图在一个页面上容纳更多的人,所以将3张图片并排是有帮助的。

这就是我的脚本本质上所做的:

[IMAGE1]

的text1

[图像2]

text2的

这就是我想要的样子: [Image1] [Image2] [Image3]

                              text1   text2   text3

为方便起见,我在下面插入了整个脚本(只有部分内容与此讨论相关)。如果你对我的要求感到困惑,请告诉我。谢谢!

var sheetID = "xxxx"; //spreadsheet
var GDoc = DocumentApp.openByUrl("xxxx");
var body = GDoc.getBody(); //google document body

function loadSheet() {
  body.clear(); //deletes previous doc contents so a new photo directory can be made

  //load studentSheet
  var StudentSheet = SpreadsheetApp.openById(sheetID).getSheetByName('Students');
  var studentdata = StudentSheet.getDataRange().getValues();

  //make variables to hold data from StudentSheet
  for (var studentRowNumber = 1; studentRowNumber < studentdata.length; studentRowNumber++) { //studentdata.length determines speed of program execution
    var FirstName = studentdata[studentRowNumber][1];
    var LastName = studentdata[studentRowNumber][2];
    var Gender = studentdata[studentRowNumber][3];
    var School = studentdata[studentRowNumber][4];
    var Grade = studentdata[studentRowNumber][5];
    var Birthday = studentdata[studentRowNumber][6];
    var StudentCellPhone = studentdata[studentRowNumber][7];
    var StudentEmail = studentdata[studentRowNumber][8];
    var DadFirstName = studentdata[studentRowNumber][9];
    var MomFirstName = studentdata[studentRowNumber][10];
    var DadLastName = studentdata[studentRowNumber][11];
    var MomLastName = studentdata[studentRowNumber][12];
    var DadEmail = studentdata[studentRowNumber][13];
    var MomEmail = studentdata[studentRowNumber][14];
    var DadCellPhone = studentdata[studentRowNumber][15];
    var MomCellPhone = studentdata[studentRowNumber][16];
    var HomePhone = studentdata[studentRowNumber][17];
    var StreetAddress = studentdata[studentRowNumber][18];
    var City = studentdata[studentRowNumber][19];
    var ZipCode = studentdata[studentRowNumber][20];  
    var longpictureID = studentdata[studentRowNumber][21];

    //verify if there is a picture uploaded for the student, and if there is then insert it in the google doc
    if (longpictureID == "") {
      Logger.log(FirstName + " " + LastName + ": No picture available"); 
    }
    else {
      insertImageFromDrive(); 
    }
    insertData();
  }
}

/** Inserts data from studentSheet variables as a paragraph beneath the image of the student **/
function insertData(){
    //insert student info into google doc
    var FullName = body.appendParagraph(FirstName + " " + LastName); //combine student's first and last names

    //verify that both parents' names are present
    if (DadFirstName == "" && MomFirstName !== "") { 
        //if dad's name is missing
        var ParentsText = body.appendParagraph("Parents: " + MomFirstName);
    }
      else {
        if (DadFirstName !== "" && MomFirstName == "") { 
        //if mom's name is missing
        var ParentsText = body.appendParagraph("Parents: " + DadFirstName);
        }
        else { 
          if (DadFirstName == "" && MomFirstName == "") {
            //if both parent names are missing
            var ParentsText = "";
          }
          else {
        //both parent names are given   
        var ParentsText = body.appendParagraph("Parents: " + DadFirstName + " & " + MomFirstName);
        }
       }
      }

    //verify that birthday is given
    if (Birthday !== "") { 
      var BirthdayText = body.appendParagraph("Birthday: " + Birthday);
    }

    //verify that grade is given
    if (Grade !== "") {
    var GradeText = body.appendParagraph("Grade: " + Grade);
    }

    //verify that both parents' phone numbers are present
    if (DadCellPhone == "" && MomCellPhone !== "") { 
        //dad's name is missing
        var CellText = body.appendParagraph("Phone: " + MomCellPhone);
    }
      else {
        if (DadCellPhone !== "" && MomCellPhone == "") { 
        //mom's name is missing
        var CellText = body.appendParagraph("Phone: " + DadCellPhone);
        }
        else { 
          if (DadCellPhone == "" && MomCellPhone == "") {
            //both parent names are missing
            var CellText = "";
        }
          else {
        //both parent names are given   
        var CellText = body.appendParagraph("Phone: " + "Dad - " + DadCellPhone + ", Mom - " + MomCellPhone);
        }
       }
      }

    //verify that both parents' emails are present
    if (DadEmail == "" && MomEmail !== "") { 
        //dad's name is missing
        var EmailText = body.appendParagraph("Phone: " + MomEmail);
    }
      else {
        if (DadEmail !== "" && MomEmail == "") { 
        //mom's name is missing
        var EmailText = body.appendParagraph("Phone: " + DadEmail);
        }
        else { 
          if (DadEmail == "" && MomEmail == "") {
            //both parent names are missing
            var EmailText = "";
        }
          else {
        //both parent names are given   
        var EmailText = body.appendParagraph("Email: " + "Dad - " + DadEmail + ", Mom - " + MomEmail);
        }
       }
      }

    // modify text attributes
    FullName.editAsText().setBold(false).setFontSize(10).setForegroundColor('#000066');

    if (ParentsText !== "") {
      ParentsText.editAsText().setFontSize(8).setForegroundColor(0, 8, '#FF0000');
    }
    if (BirthdayText !== "") {
      BirthdayText.editAsText().setFontSize(8).setForegroundColor(0, 9, '#FF0000');
    }

    GradeText.editAsText().setFontSize(8).setForegroundColor(0, 6, '#FF0000');

    if (CellText !== "") {
      CellText.editAsText().setFontSize(8).setForegroundColor(0, 6, '#FF0000'); //makes first 6 characters red ("Phone:")
    }
    if (EmailText !== "") {
    EmailText.editAsText().setFontSize(8).setForegroundColor(0, 6, '#FF0000');
    }
    GDoc.appendHorizontalRule(); //not to be used if the text and image are in a table
}

/** Inserts photo of the student from google drive **/
function insertImageFromDrive(){
  var shortpictureID = longpictureID.replace('https://drive.google.com/uc?export=view&id=', ''); 
  //(old, new); replace all occurences of old with new in string
  var img = DriveApp.getFileById(shortpictureID).getBlob();
  var inlineI = GDoc.appendImage(img);

  //resizing the image
  var width = inlineI.getWidth();
  var newW = width;
  var height = inlineI.getHeight();
  var newH = height;
  var ratio = width/height;
    if(width>120){ 
      //max width of image
      newW = 120; 
      newH = parseInt(newW/ratio);
    }
  inlineI.setWidth(newW).setHeight(newH);
} 

1 个答案:

答案 0 :(得分:1)

为此,您可以创建一个单元格变量

   var cells = [['Row 1, Cell 1', 'Row 1, Cell 2', 'Row 1, Cell 3'],
                ['Row 2, Cell 1', 'Row 2, Cell 2', 'Row 2, Cell 3']];
  1. 第1行可以是3名学生的图像。
  2. 第2行可以是您要添加的文字。

    您也可以将边框宽度设置为0。

    body.appendTable(细胞).setBorderWidth(0);

  3. 希望有所帮助!