2

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

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

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

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

[图片1]

文本1

[图2]

文本2

这就是我想要的样子:[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);
} 
4

1 回答 1

2

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

   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(cells).setBorderWidth(0);

希望有帮助!

于 2014-11-19T20:18:37.753 回答