[英]Google Slides API - Text alignment CENTER and contentAlignment MIDDLE
I'm trying to programmatically add hundreds of text boxes to a Google Slides presentation.我正在尝试以编程方式将数百个文本框添加到 Google 幻灯片演示文稿中。 I would like to set the text center horizontal aligned and middle vertical aligned.
我想设置文本中心水平对齐和中间垂直对齐。 Can anyone give an example of doing so with the text of a text box.
任何人都可以举一个使用文本框文本这样做的例子。 I have tried the suggested API request in just about every position in my requests text:
我几乎在我的请求文本中的每个位置都尝试了建议的 API 请求:
'ContentAlignment': 'MIDDLE'
& &
'alignment': 'CENTER'
Where would I put these lines in the code below?我会将这些行放在下面的代码中的什么位置?
def add_text_box(ss, org, elemID, presID):
# Create a new square textbox, using the supplied element ID.
height = {
'magnitude': 50,
'unit': 'PT'
}
width = {
'magnitude': 200,
'unit': 'PT'
}
requests = []
requests.append(
{
'createShape': {
'objectId': elemID,
'shapeType': 'TEXT_BOX',
'elementProperties': {
'pageObjectId': org,
'size': {
'height': height,
'width': width
},
'transform': {
'scaleX': 1,
'scaleY': 1,
'translateX': 10,
'translateY': 10,
'unit': 'PT'
}
}
}
}
)
# Insert text into the box, using the supplied element ID.
requests.append(
{
'insertText': {
'objectId': elemID,
'insertionIndex': 0,
'text': 'Position\nName\nDate'
}
}
)
# Change text style based on position in text string
requests.append(
{
'updateTextStyle': {
'objectId': elemID,
'textRange': {
'type': 'FIXED_RANGE',
'startIndex': 0,
'endIndex': 8
},
'style': {
'fontFamily': 'Arial',
'fontSize': {
'magnitude': 10,
'unit': 'PT'
},
},
'fields': 'fontFamily,fontSize'
}
}
)
requests.append(
{
'updateTextStyle': {
'objectId': elemID,
'textRange': {
'type': 'FIXED_RANGE',
'startIndex': 9,
'endIndex': 13
},
'style': {
'fontFamily': 'Arial',
'bold': True,
'fontSize': {
'magnitude': 14,
'unit': 'PT'
},
},
'fields': 'fontFamily,bold,fontSize'
}
}
)
requests.append(
{
'updateTextStyle': {
'objectId': elemID,
'textRange': {
'type': 'FIXED_RANGE',
'startIndex': 14,
'endIndex': 18
},
'style': {
'fontFamily': 'Arial',
'fontSize': {
'magnitude': 8,
'unit': 'PT'
},
},
'fields': 'fontFamily,fontSize'
}
}
)
# Execute the request.
body = {
'requests': requests
}
response = ss.presentations().batchUpdate(presentationId=presID, body=body).execute()
create_shape_response = response.get('replies')[0].get('createShape')
print('Created textbox with ID: {0}'.format(create_shape_response.get('objectId')))
OR code in another requests
string.另一个
requests
字符串中的 OR 代码。
To get the text in the shape to center horizontally, add the following in the original code:要使形状中的文本水平居中,请在原始代码中添加以下内容:
requests.append(
{
'updateParagraphStyle': {
"objectId": elemID,
"style": {
"alignment": "CENTER"
},
"fields": 'alignment',
}
}
)
To get the text in the shape to position vertically in the MIDDLE, as well as draw a solid outline and fill the shape, add the following in the original code:要使形状中的文本在 MIDDLE 中垂直定位,以及绘制实心轮廓并填充形状,请在原始代码中添加以下内容:
requests.append(
{
"updateShapeProperties": {
"objectId": elemID,
"fields": "outline,shapeBackgroundFill,contentAlignment",
"shapeProperties": {
"shapeBackgroundFill": {
"solidFill": {
"alpha": 0.6,
"color": {
"themeColor": "ACCENT5"
}
}
},
"outline": {
"dashStyle": "SOLID",
"outlineFill": {
"solidFill": {
"alpha": 1,
"color": {
"themeColor": "ACCENT5"
}
}
},
"weight": {
"magnitude": 3,
"unit": "PT"
}
},
"contentAlignment": 'MIDDLE'
}
}
}
)
One of the comments asked for a working solution.其中一条评论要求提供可行的解决方案。 This is in Google batchUpdate.
这是在 Google batchUpdate 中。 A student roster is read and a slide is created for each student with the student's name in a text box at the upper left.
读取学生名册并为每个学生创建一张幻灯片,左上角的文本框中显示学生姓名。 I anticipate changing out a master or background to create different assignments.
我预计更换一个主人或背景来创建不同的作业。
function createSlideDeck(studentObj, assignNm, presId) { console.log('= = = = = = = = = = Begin createSlideDeck for ', assignNm, ' = = = = = = = = = = '); const ui = SpreadsheetApp.getUi();
const ss = SpreadsheetApp.getActiveSpreadsheet(); const ssId = ss.getId();
// - - - - - - - - - - - - - create page for each student - - - - -
- - - - - - - - console.log('# students: ', studentObj.length); let updtReqArr = []
, pageId, pageElementId
, inObj = {}
, slideObj = {};
for (let i = 0; i < studentObj.length; i++) {
console.log('+ + + + + + + + + i: ', i, ' + + + + + + + + +');
console.log('student: ', studentObj[i].first);
// add a slide
pageId = Utilities.getUuid();
console.log('pageId: ', pageId);
slideObj = [{
'createSlide': {
'objectId': pageId,
'insertionIndex': 1,
'slideLayoutReference': {
'predefinedLayout': 'BLANK' // name of master
}
}
}];
console.log('slideObj: ', JSON.stringify(slideObj));
updtReqArr.push(slideObj);
console.log('updtReqArr.length: ', updtReqArr.length);
let sORi = 'solid';
inObj = { 'red': 0.988, 'green': 0.97, 'blue': 0.87, 'alpha': 0.3 };
bkgrdStyle = createBkgrdStyle(pageId, sORi, inObj);
updtReqArr.push(bkgrdStyle);
console.log('bkgrdStyle: ', JSON.stringify(bkgrdStyle));
console.log('updtReqArr.length: ', updtReqArr.length);
// add a shape
pageElementId = Utilities.getUuid();
// console.log('pageElementId: ', pageElementId );
let shapeEnum = 'TEXT_BOX';
inObj = {
'size': { 'height': 35, 'width': 100, 'top': 10, 'left': 10 }
,'text': studentObj[i].first
,'shapeFill': { 'red': 0.988, 'green': 0.97, 'blue': 0.87, 'alpha': 0 }
,'border': { 'red': 0.988, 'green': 0.97, 'blue': 0.87, 'alpha': 0, 'weight': 0 }
,'txtProperties': { 'fntFam': "Corsiva", 'fntSz': 16,
'red': 0.988, 'green': 0.97, 'blue': 0.87,
'redT': 0.058, 'greenT': 0.045, 'blueT': 0.176 }
};
shapePkg = createShapeObj(pageId, pageElementId, shapeEnum, inObj)
updtReqArr.push(shapePkg.createShape);
console.log('createShape: ', JSON.stringify(shapePkg.createShape));
console.log('updtReqArr.length: ', updtReqArr.length);
updtReqArr.push(shapePkg.updtShpProps);
console.log('updtShpProps: ', JSON.stringify(shapePkg.updtShpProps));
console.log('updtReqArr.length: ', updtReqArr.length);
updtReqArr.push(shapePkg.updtTxtStyle);
console.log('updtTxtStyle: ', JSON.stringify(shapePkg.updtTxtStyle));
console.log('updtReqArr.length: ', updtReqArr.length);
// pres.addEditor(studentObj[i].schoolEmail); // created with batchUpdate after all students
console.log('slide set up for add: ', studentObj[i].first);
console.log('end of a student - updtReqArr.length: ', updtReqArr.length);
response = Slides.Presentations.batchUpdate({ 'requests': updtReqArr }, presId);
console.log('response: ', response );
updtReqArr = []; } // end loop for each student
console.log('after student loop updtReqArr.length: ', updtReqArr.length); // response = Slides.Presentations.batchUpdate({ 'requests': updtReqArr }, presId); // console.log('response: ', response ); DriveApp.getFileById(presId)
.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
console.log('End createSlideDeck'); }
THIS ALL RUN AT ONCE这一切同时运行
/**
NOTES: 'red':'num','green':'num', 'blue':'num', 'alpha':'num'}
where num is a value between 1.0 and 0.0
get this number by dividing the 0 - 255 number by 255
alpha is the value for alpha. a value of 1.0 corresponds to a
solid color, whereas a value of 0.0 corresponds to a completely
alphaparent color. Use alpha to get lighter colors.
paramaters
pageId: id of individual slide
shapeEnum: 'solid' color or 'image'
inObj= {
'size': {
'height':num
,'width':num
,'top':num
,'left':num
},
,'text':string
,shapeFill: {
,'red'
,'green'
,'blue'
,'alpha'
},
,border: {
,'red'
,'green'
,'blue'
,'alpha'
,'weight'
}
. . . . stuff used to set properties
}
returns shapeObj
*/
function createShapeObj(pageId, pageElementId, shapeEnum, inObj) {
console.log('Begin createShapeObj - inObj.text: ', inObj.text);
let shapeObj = [{
'createShape': {
'objectId': pageElementId,
'shapeType': 'TEXT_BOX',
'elementProperties': {
'pageObjectId': pageId,
'size': {
'width': {
'magnitude': inObj.size.width,
'unit': 'PT'
},
'height': {
'magnitude': inObj.size.height,
'unit': 'PT'
}
}, // end size
'transform': {
'scaleX': 1,
'scaleY': 1,
'translateX': inObj.size.left,
'translateY': inObj.size.top,
'unit': 'PT'
}
} // end elementProperties
} // end createShape
}, {
'insertText': {
'objectId': pageElementId
,'text': inObj.text
,'insertionIndex': 0
}
}];
// set shape properties
shapeUpdObj = [{}];
shapeUpdObj = updtShpProps(pageElementId, inObj);
// style the text
txtStyleObj = [{}];
txtStyleObj = updtTxtStyle(pageElementId, inObj);
shapePkg = { 'createShape': shapeObj, 'updtShpProps': shapeUpdObj, 'updtTxtStyle': txtStyleObj };
return shapePkg;
}
THE NEXT CHUNK OF CODE下一块代码
function updtShpProps(pageElementId, inObj) {
shapeUpdObj = [{
'updateShapeProperties': {
'objectId': pageElementId
,"fields": "shapeBackgroundFill.solidFill.color"
,"shapeProperties": {
"contentAlignment": 'MIDDLE'
,"shapeBackgroundFill": {
"propertyState": 'RENDERED'
,"solidFill": {
"color": {
'rgbColor': {
"red": inObj.shapeFill.red,
"green": inObj.shapeFill.green,
"blue": inObj.shapeFill.blue
}
},
"alpha": inObj.shapeFill.trans
}
},
"outline": { // border
"outlineFill": {
"solidFill": {
"color": {
'rgbColor': {
"red": inObj.border.red,
"green": inObj.border.green,
"blue": inObj.border.blue
}
},
"alpha": inObj.border.trans
}
},
"weight": {
"magnitude": inObj.border.weight,
"unit": 'PT'
}
} // end outline / border properties
} // end shapeProperties
}
}, { // end updateShapeProperties
'updateParagraphStyle': {
'objectId': pageElementId
,'textRange': {
'type': "ALL"
}
,'style': {
"alignment": "CENTER"
}
,'fields':'*'
}
} // end update paragraph style
]; // end shapeUpdObj
return shapeUpdObj;
}
THIS IS THE LAST BIT这是最后一点
function updtTxtStyle(pageElementId, inObj) {
txtStyleObj = [{
'updateTextStyle': {
'objectId': pageElementId
,'fields': 'foregroundColor,bold,italic,fontFamily,fontSize,underline'
,'style': {
// This did not seem necessary but it ran successfully
// "backgroundColor": {
// 'opaqueColor': {
// "rgbColor": {
// "red": inObj.red,
// "green": inObj.green,
// "blue": inObj.blue
// }
// }
// }
// ,'foregroundColor': {
// 'opaqueColor': {
// "rgbColor": {
// "red": inObj.redT,
// "green": inObj.greenT,
// "blue": inObj.blueT
// }
// }
// }
// 'themeColor': 'ACCENT5' // https://developers.google.com/slides/reference/rest/v1/presentations.pages/other
'bold': true
,'italic': false
,'underline': false
,'fontFamily': inObj.txtProperties.fntFam
,'fontSize': {
'magnitude': inObj.txtProperties.fntSz
,'unit': 'PT'
}
}
,'textRange': {
'type': 'ALL'
}
} // end update text style
}]
return txtStyleObj;
}
This took a long time but I succeeded by drilling down through the documentation step at a time, testing with each addition.这花了很长时间,但我成功地通过一次深入文档步骤,对每个添加进行了测试。 Good luck to everyone.
祝大家好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.