【发布时间】:2015-03-11 21:53:50
【问题描述】:
上下文:我有一个程序可以在 google 文档中为 6-12 年级的学生和领导/教师创建照片目录。对于每个学生,它会插入一张图片,然后是几行单独的文本(姓名、电话号码等)。在每个新等级开始之前,插入文本以标记等级(即 6 年级)。当过渡到新年级时(即所有 6 年级学生都在目录中,现在从 7 年级学生开始),将开始一个新页面并添加另一个标题来标记新年级(即 7 年级) .我想把它做成一个 3 列目录,每列有 4 名学生。由于谷歌文档不允许我直接制作列,我相信我需要制作一个带有不可见/白色边框的表格。我想让每一页成为一个 4 行 x 3 列的表。有些年级有超过 12 名学生(一张 4x3 桌子的价值),所以对于某些年级,我需要继续阅读多个页面。对于没有 12 名学生的页面,我需要将页面上的其余单元格设为空白,或者进行分页,以便新成绩从单独的页面开始。
这是主要问题:如何将图像和段落“附加”到表格中的特定单元格?我将如何修改下面的代码,以便在表格单元格中插入学生的照片和多行信息,然后启动一个新的表格单元格?我尝试制作一个表格,我可以在其中访问每个单元格作为数组的一部分,然后将图像和段落附加到特定单元格,但我不知道如何(错误代码未包含在下面)。
抱歉,代码太长了。如果您需要澄清,请告诉我。谢谢!
var sheetID = "x"; //x = link
var GDoc = DocumentApp.openByUrl("y"); //y = link
var body = GDoc.getBody(); //google document body
function loadSheet() {
body.clear(); //deletes previous doc contents so a new photo directory can be made
//** Variables **//
//load studentSheet
var StudentSheet = SpreadsheetApp.openById(sheetID).getSheetByName('Students');
var studentdata = StudentSheet.getDataRange().getValues();
var PreviousGrade = "0"; //initially sets the previous grade to zero so that the first grade created will be different than the previous grade
//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 MemberCellPhone = studentdata[studentRowNumber][7]; //student or leader cell phone
var MemberEmail = 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];
/** determines whether a page break should be inserted for a transition to the next grade **/
var CurrentGrade = Grade;
if (CurrentGrade !== PreviousGrade) { //new grade category
if (PreviousGrade !== "0") { //this is not the first grade category
GDoc.appendPageBreak();
}
if (PreviousGrade == "0") { //this is the first grade category
var LittleBlankLine = body.appendParagraph(""); //blank line inserted before first age category title to make things line up evenly on document with columns; modify as needed
}
if (CurrentGrade == "6" || CurrentGrade == "7" || CurrentGrade == "8") { //6-8th; || = or
var GradeCategory = "MS";
var GradeCategoryTitle = body.appendParagraph(GradeCategory);
var NewGradeTitle = body.appendParagraph("Grade " + CurrentGrade);
var BigBlankLine = body.appendParagraph("");
var SeparationLine = GDoc.appendParagraph("_________________________");
PreviousGrade = CurrentGrade;
}
if (CurrentGrade == "9" || CurrentGrade == "10" || CurrentGrade == "11" || CurrentGrade == "12") { //9-12th
var GradeCategory = "HS";
var GradeCategoryTitle = body.appendParagraph(GradeCategory);
var NewGradeTitle = body.appendParagraph("Grade " + CurrentGrade);
var BigBlankLine = body.appendParagraph("");
var SeparationLine = GDoc.appendParagraph("_________________________");
PreviousGrade = CurrentGrade;
}
if (CurrentGrade == "Leader") {
var GradeCategory = "Youth";
var GradeCategoryTitle = body.appendParagraph(GradeCategory);
var NewGradeTitle = body.appendParagraph("Leaders");
var BigBlankLine = body.appendParagraph("");
var SeparationLine = GDoc.appendParagraph("_________________________");
PreviousGrade = CurrentGrade;
}
GradeCategoryTitle.editAsText().setFontSize(36);
NewGradeTitle.editAsText().setFontSize(36);
var LittleBlankLine = body.appendParagraph(""); //blank line
BigBlankLine.editAsText().setFontSize(36);
SeparationLine.editAsText().setFontSize(7);
}
//** Inserting Picture of Student into Google Doc **//
//verify if there is a picture uploaded for the student, and if there is then insert it in the google doc
if (longpictureID !== "") { //there is an uploaded picture
var shortpictureID = longpictureID.replace('https://drive.google.com/uc?export=view&id=', '');
//(old, new); replace all occurences of old with new in string
}
else { //there is not an uploaded picture
shortpictureID = "0B5kYlCqpy3BBX2M4M2dWVWEzcjA"; //pic of a silhoutte
}
//insert image from drive
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;
//this makes the images all the same height
newH = 60;
newW = parseInt(newH/(1/ratio));
inlineI.setWidth(newW).setHeight(newH);
//** 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 = body.appendParagraph("Parents: ");
}
else {
//both parent names are given
var ParentsText = body.appendParagraph("Parents: " + DadFirstName + " & " + MomFirstName);
}
}
}
//verify that birthday is given
if (Birthday !== "") { //birthday is given
var BirthdayText = body.appendParagraph("Birthday: " + Birthday);
}
else { //no birthday is given
var BirthdayText = body.appendParagraph("Birthday: ");
}
//verify that grade is given
if (Grade !== "") {
if (Grade == "Leader") { //this is a leader
var GradeText = body.appendParagraph("Role: " + Grade);
}
else { //this is a student
var GradeText = body.appendParagraph("Grade: " + Grade);
}
}
else { //no grade is given
var GradeText = body.appendParagraph("Grade: ");
}
//determine whether this is a student or leader, and if student then verify that both parents' phone numbers are present
if (Grade == "Leader" && MemberCellPhone !== "") { //this is a leader and he/she has a cellphone
var CellTextLabel = body.appendParagraph("Phone: "); //label
var MemberCellText = body.appendParagraph(MemberCellPhone); //leader's cell phone number
var DadCellText = "";
var MomCellText = "";
//extra blank line is added for email
}
else { //this is a student
if (DadCellPhone == "" && MomCellPhone !== "") {
//dad's name is missing
var CellTextLabel = body.appendParagraph("Phone: ");
var MomCellText = body.appendParagraph(" Mom - " + MomCellPhone);
var LittleBlankLine = body.appendParagraph(""); //blank line
var MemberCellText = "";
var DadCellText = "";
}
else {
if (DadCellPhone !== "" && MomCellPhone == "") {
//mom's name is missing
var CellTextLabel = body.appendParagraph("Phone: ");
var DadCellText = body.appendParagraph(" Dad - " + DadCellPhone);
var LittleBlankLine = body.appendParagraph(""); //blank line
var MemberCellText = "";
var MomCellText = "";
}
else {
if (DadCellPhone == "" && MomCellPhone == "") {
//both parent names are missing
var CellTextLabel = body.appendParagraph("Phone: None");
var LittleBlankLine = body.appendParagraph("");
var LittleBlankLine = body.appendParagraph("");
var MemberCellText = "";
var DadCellText = "";
var MomCellText = "";
}
else {
//both parent names are given
var CellTextLabel = body.appendParagraph("Phone: ");
var DadCellText = body.appendParagraph(" Dad - " + DadCellPhone);
var MomCellText = body.appendParagraph(" Mom - " + MomCellPhone);
var MemberCellText = "";
}
}
}
}
//verify that both parents' emails are present
if (Grade == "Leader" && MemberEmail !== "") { //this is a leader and he/she has an email
var EmailTextLabel = body.appendParagraph("Email: "); //leader's email
var MemberEmailText = body.appendParagraph(" " + MemberEmail);
var LittleBlankLine = body.appendParagraph("");
var LittleBlankLine = body.appendParagraph(""); //extra blank line for leader email, because there is one less blank line for phone
var DadEmailText = "";
var MomEmailText = "";
}
else { //this is a student
if (DadEmail == "" && MomEmail !== "") {
//dad's name is missing
var EmailTextLabel = body.appendParagraph("Email: ");
var MomEmailText = body.appendParagraph(" Mom - " + MomEmail);
var LittleBlankLine = body.appendParagraph("");
var DadEmailText = "";
var MemberEmailText = "";
}
else {
if (DadEmail !== "" && MomEmail == "") {
//mom's name is missing
var EmailTextLabel = body.appendParagraph("Email: ");
var DadEmailText = body.appendParagraph(" Dad - " + DadEmail);
var LittleBlankLine = body.appendParagraph("");
var MomEmailText = "";
var MemberEmailText = "";
}
else {
if (DadEmail == "" && MomEmail == "") {
//both parent names are missing
var EmailTextLabel = body.appendParagraph("Email: None");
var LittleBlankLine = body.appendParagraph("");
var LittleBlankLine = body.appendParagraph("");
var DadEmailText = "";
var MomEmailText = "";
var MemberEmailText = "";
}
else {
//both parent names are given
var EmailTextLabel = body.appendParagraph("Email: ");
var DadEmailText = body.appendParagraph(" Dad - " + DadEmail);
var MomEmailText = body.appendParagraph(" Mom - " + MomEmail);
var MemberEmailText = "";
}
}
}
}
//** modify text attributes **//
if (FullName !== "") {
FullName.editAsText().setBold(false).setFontSize(9).setForegroundColor('#000066'); //black
}
if (ParentsText !== "") {
ParentsText.editAsText().setFontSize(7).setForegroundColor(0, 8, '#FF0000'); //red
}
if (BirthdayText !== "") {
BirthdayText.editAsText().setFontSize(7).setForegroundColor(0, 9, '#FF0000');
}
if (GradeText !== "") {
if (Grade == "Leader") { //this is a leader
GradeText.editAsText().setFontSize(7).setForegroundColor(0, 5, '#FF0000');
}
else { //this is a student
GradeText.editAsText().setFontSize(7).setForegroundColor(0, 6, '#FF0000');
}
}
if (CellTextLabel !== "") {
CellTextLabel.editAsText().setFontSize(7).setForegroundColor(0, 6, '#FF0000'); //makes first 6 characters red ("Phone:")
}
if (MemberCellText !== "") {
MemberCellText.editAsText().setFontSize(7).setForegroundColor('#000066'); //black
}
if (DadCellText !== "") {
DadCellText.editAsText().setFontSize(7).setForegroundColor('#000066');
}
if (MomCellText !== "") {
MomCellText.editAsText().setFontSize(7).setForegroundColor('#000066');
}
if (EmailTextLabel !== "") {
EmailTextLabel.editAsText().setFontSize(7).setForegroundColor(0, 6, '#FF0000'); //student's email, red
}
if (MemberEmailText !== "") {
MemberEmailText.editAsText().setFontSize(7).setForegroundColor('#000066'); //black
}
if (DadEmailText !== "") {
DadEmailText.editAsText().setFontSize(7).setForegroundColor('#000066');
}
if (MomEmailText !== "") {
MomEmailText.editAsText().setFontSize(7).setForegroundColor('#000066');
}
//if (LittleBlankLine !== null) { //not sure if this conditional works
LittleBlankLine.editAsText().setFontSize(7);
//}
var SeparationLine = GDoc.appendParagraph("_________________________");
SeparationLine.editAsText().setFontSize(7);
GDoc.appendParagraph("");
}
}
【问题讨论】:
标签: arrays google-apps-script google-docs multiple-columns tablecell