- When the textbox of
textBoxID
is not existing, you want to create new textbox.
- When the textbox of
textBoxID
is existing, you want to modify the value in the textbox.
- About creating the textbox, you want to use the function of
newTextBox
.
- You want to achieve this using Google Apps Script.
- You have already been able to create new textbox using your script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Sample situation:
In this modification, as a sample situation, the initial value of textbox is 0
. And when the textbox is not existing in the slide, new textbox is created. When the textbox is existing in the slide, the value of textbox is modified. In this case, 1
is added to the existing value.
Pattern 1:
In this pattern, your script of newTextBox
is used. Before you use this, please set the variables of presentationId
, pageId
and data
. data
is a sample value.
Sample script:
// Please set following variables.
const presentationId = "###";
const pageId = "###";
function newTextBox(textBoxID, xpos, ypos, textBoxText) {
var elementId = textBoxID;
var requests = [
{createShape: {objectId: textBoxID, shapeType: 'TEXT_BOX', elementProperties: {pageObjectId: pageId, size: {width: {magnitude: 53.5748, unit: 'PT'}, height: {magnitude: 22.6772, unit: 'PT'}}, transform: {scaleX: 1, scaleY: 1, translateX: xpos, translateY: ypos, unit: 'PT'}}}},
{insertText: {objectId: textBoxID, insertionIndex: 0, text: textBoxText}}
];
var createTextboxWithTextResponse = Slides.Presentations.batchUpdate({requests: requests}, presentationId);
var createShapeResponse = createTextboxWithTextResponse.replies[0].createShape;
Logger.log('Created textbox with ID: %s', createShapeResponse.objectId);
}
// Please run this function.
function myFunction() {
var data = [
{textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
{textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
{textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
];
const slide = SlidesApp.openById(presentationId).getSlideById(pageId);
const obj = slide.getShapes().reduce((o, e) => {
o[e.getObjectId()] = e;
return o;
}, {});
ar.forEach(({textBoxID, xpos, ypos, textBoxText}) => {
if (textBoxID in obj) {
const oldValue = obj[textBoxID].getText().asString(); // Here, you can retrieve the value of existing textbox.
const newValue = Number(oldValue) + 1;
obj[textBoxID].getText().replaceAllText(oldValue, newValue);
} else {
newTextBox(textBoxID, xpos, ypos, textBoxText);
}
});
}
Pattern 2:
In this pattern, one request body is created using the values of data
and use it to the method of batchUpdate. By this, only one API call is used. Before you use this, please set the variables of presentationId
, pageId
and data
. data
is a sample value.
Sample script:
function myFunction() {
// Please set following variables.
var data = [
{textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
{textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
{textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
];
const presentationId = "###";
const pageId = "###";
const slide = SlidesApp.openById(presentationId).getSlideById(pageId);
const obj = slide.getShapes().reduce((o, e) => {
o[e.getObjectId()] = e;
return o;
}, {});
const requests = data.reduce((ar, {textBoxID, xpos, ypos, textBoxText}) => {
if (textBoxID in obj) {
const oldValue = obj[textBoxID].getText().asString(); // Here, you can retrieve the value of existing textbox.
const newValue = Number(oldValue) + 1;
obj[textBoxID].getText().replaceAllText(oldValue, newValue);
} else {
ar.push([
{createShape: {objectId: textBoxID, shapeType: 'TEXT_BOX', elementProperties: {pageObjectId: pageId, size: {width: {magnitude: 53.5748, unit: 'PT'}, height: {magnitude: 22.6772, unit: 'PT'}}, transform: {scaleX: 1, scaleY: 1, translateX: xpos, translateY: ypos, unit: 'PT'}}}},
{insertText: {objectId: textBoxID, insertionIndex: 0, text: textBoxText}}
]);
}
return ar;
}, []);
if (requests.length > 0) {
var createTextboxWithTextResponse = Slides.Presentations.batchUpdate({requests: requests}, presentationId);
Logger.log(createTextboxWithTextResponse)
}
}
Note:
- In your script,
console.log
is used. So I thought that your script editor might be using with enabling V8 runtime. So I modified the script as V8. Please be careful this.
- This is a simple modified script. So please modify this for your actual situation.
References:
If I misunderstood your question and this was not the direction you want, I apologize.