Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌应用脚本:如何在谷歌幻灯片上创建幻灯片,并用数组中的信息填充其中的占位符?_Javascript_Google Apps Script_Google Slides Api_Google Slides - Fatal编程技术网

Javascript 谷歌应用脚本:如何在谷歌幻灯片上创建幻灯片,并用数组中的信息填充其中的占位符?

Javascript 谷歌应用脚本:如何在谷歌幻灯片上创建幻灯片,并用数组中的信息填充其中的占位符?,javascript,google-apps-script,google-slides-api,google-slides,Javascript,Google Apps Script,Google Slides Api,Google Slides,我有一个数组,正在尝试使用其中的信息创建一个谷歌幻灯片演示文稿。我想得到一些帮助,如果可能的话,还可以给我指一些我可以阅读的材料,以便更好地理解这些问题的可能解决方案 阵列如下所示: var myArray = [[Project1,Status1,Info1],[Project2,Status2,Info2],...,[ProjectN,StatusN,InfoN]] function CreatingSlides() { var displayName = "NEW LAYOUT"; /

我有一个数组,正在尝试使用其中的信息创建一个谷歌幻灯片演示文稿。我想得到一些帮助,如果可能的话,还可以给我指一些我可以阅读的材料,以便更好地理解这些问题的可能解决方案

阵列如下所示:

var myArray = [[Project1,Status1,Info1],[Project2,Status2,Info2],...,[ProjectN,StatusN,InfoN]]
function CreatingSlides() {

var displayName = "NEW LAYOUT"; // Name of the layout that I created
var presentationId = SlidesApp.openById(PageId).getId(); //It is an existing presentation, so I just get its ID
var layouts = Slides.Presentations.get(presentationId).layouts;
var layout = {};

//The following is the array of elements. In this example,
//I only used 4 projects, but it may vary and may not be
//a multiple of 4.
var myArray = [["Project1","Status1","Info1"],["Project2","Status2","Info2"],["Project3","Status3","Info3"],["Project4","Status4","Info4"]];


//This loop searches for the layout that I created
for (i in layouts) {
  layout[layouts[i].layoutProperties.displayName] = layouts[i].objectId;
  }

//In the slide created, this loop will fill the information in it using the myArray
for (i in myArray) {
  var resource = {"requests": [{"createSlide": {"slideLayoutReference": {"layoutId": layout[displayName]},
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 0},"objectId":myArray[i][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 0},"objectId":myArray[i][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 1},"objectId":myArray[i][3],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 1},"objectId":myArray[i+1][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 2},"objectId":myArray[i+1][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 3},"objectId":myArray[i+1][3],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 2},"objectId":myArray[i+2][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 4},"objectId":myArray[i+2][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 5},"objectId":myArray[i+2][3],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 3},"objectId":myArray[i+3][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 6},"objectId":myArray[i+3][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 7},"objectId":myArray[i+3][3],},],
                                                }
                                },
                 {"insertText": {"objectId": myArray[i][1],"text": myArray[i][1],},
                  "insertText": {"objectId": myArray[i][2],"text": myArray[i][2],},
                  "insertText": {"objectId": myArray[i][3],"text": myArray[i][3],},
                  "insertText": {"objectId": myArray[i+1][1],"text": myArray[i+1][1],},
                  "insertText": {"objectId": myArray[i+1][2],"text": myArray[i+1][2],},
                  "insertText": {"objectId": myArray[i+1][3],"text": myArray[i+1][3],},
                  "insertText": {"objectId": myArray[i+2][1],"text": myArray[i+2][1],},
                  "insertText": {"objectId": myArray[i+2][2],"text": myArray[i+2][2],},
                  "insertText": {"objectId": myArray[i+2][3],"text": myArray[i+2][3],},
                  "insertText": {"objectId": myArray[i+3][1],"text": myArray[i+3][1],},
                  "insertText": {"objectId": myArray[i+3][2],"text": myArray[i+3][2],},
                  "insertText": {"objectId": myArray[i+3][3],"text": myArray[i+3][3],}},
                               ]};

Slides.Presentations.batchUpdate(resource, presentationId);   

}

}
数组中的每个元素都引用不同的项目,并包含:

  • 项目名称
  • 项目的状态
  • 关于这个项目的一些随机信息
它们都是字符串。数组中的元素数量可能会有所不同,但它们始终遵循此结构

在演示中,我在主布局中创建了一个新布局,我将其命名为“new layout”。该布局有4个组,每个组中有3个占位符。每组占位符都应该填充不同项目的信息,这意味着一组占位符将填充
myArray[i][1]
myArray[i][2]
myArray[i][3]

到目前为止,我的代码如下:

var myArray = [[Project1,Status1,Info1],[Project2,Status2,Info2],...,[ProjectN,StatusN,InfoN]]
function CreatingSlides() {

var displayName = "NEW LAYOUT"; // Name of the layout that I created
var presentationId = SlidesApp.openById(PageId).getId(); //It is an existing presentation, so I just get its ID
var layouts = Slides.Presentations.get(presentationId).layouts;
var layout = {};

//The following is the array of elements. In this example,
//I only used 4 projects, but it may vary and may not be
//a multiple of 4.
var myArray = [["Project1","Status1","Info1"],["Project2","Status2","Info2"],["Project3","Status3","Info3"],["Project4","Status4","Info4"]];


//This loop searches for the layout that I created
for (i in layouts) {
  layout[layouts[i].layoutProperties.displayName] = layouts[i].objectId;
  }

//In the slide created, this loop will fill the information in it using the myArray
for (i in myArray) {
  var resource = {"requests": [{"createSlide": {"slideLayoutReference": {"layoutId": layout[displayName]},
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 0},"objectId":myArray[i][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 0},"objectId":myArray[i][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 1},"objectId":myArray[i][3],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 1},"objectId":myArray[i+1][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 2},"objectId":myArray[i+1][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 3},"objectId":myArray[i+1][3],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 2},"objectId":myArray[i+2][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 4},"objectId":myArray[i+2][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 5},"objectId":myArray[i+2][3],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 3},"objectId":myArray[i+3][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 6},"objectId":myArray[i+3][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 7},"objectId":myArray[i+3][3],},],
                                                }
                                },
                 {"insertText": {"objectId": myArray[i][1],"text": myArray[i][1],},
                  "insertText": {"objectId": myArray[i][2],"text": myArray[i][2],},
                  "insertText": {"objectId": myArray[i][3],"text": myArray[i][3],},
                  "insertText": {"objectId": myArray[i+1][1],"text": myArray[i+1][1],},
                  "insertText": {"objectId": myArray[i+1][2],"text": myArray[i+1][2],},
                  "insertText": {"objectId": myArray[i+1][3],"text": myArray[i+1][3],},
                  "insertText": {"objectId": myArray[i+2][1],"text": myArray[i+2][1],},
                  "insertText": {"objectId": myArray[i+2][2],"text": myArray[i+2][2],},
                  "insertText": {"objectId": myArray[i+2][3],"text": myArray[i+2][3],},
                  "insertText": {"objectId": myArray[i+3][1],"text": myArray[i+3][1],},
                  "insertText": {"objectId": myArray[i+3][2],"text": myArray[i+3][2],},
                  "insertText": {"objectId": myArray[i+3][3],"text": myArray[i+3][3],}},
                               ]};

Slides.Presentations.batchUpdate(resource, presentationId);   

}

}
问题1:当数组中的元素数量可能不同时,如何执行此操作?

在这段代码中,我只使用myArray中的4个元素,因此创建的幻灯片中的所有占位符都将被填充。但是,如果元素数大于4,我不明白如何告诉它创建新幻灯片。此外,元素的数量不能是4的倍数(可以是5,可以是60,可以是72…)

问题2:我不知道如何正确使用请求中的objectID

我从谷歌应用程序脚本参考中看到的另一段代码中得到了这一部分。在其中,我了解到,在请求内部,if首先为objectId设置一个名称,然后使用该名称在占位符中写入内容。但是,这会产生一些问题:objectId有时不接受我试图给它的名称,或者objectId说它不能使用我建议的名称,因为它应该是唯一的。有更好的方法吗

问题3:是否可以不用写12行来填充占位符?


我认为这只是因为我不知道如何正确使用ObjectID(问题2)。因此,我必须分别命名幻灯片中的每个objectId。我相信有一些方法可以使用循环来实现这一点,但到目前为止,我还无法找到解决方案或理解它。

首先有一个建议:您使用的是和内置的混合。如果只使用SlidesApp,会简单得多,因为它可以满足您的需要,而且您不必担心对象是否正确

回答您的问题:

问题1:当数组中的元素数量可能不同时,如何执行此操作? 这里的问题是布局中只有12个占位符。尝试填充超过该数量的内容是行不通的,因为您将用完要填充的占位符。这里的选项是:不要使用占位符,而是创建和定位文本框,或者以不同的方式显示数据。例如,每个项目使用一张幻灯片,其中包含3个占位符,一个用于项目名称、状态和信息

我建议您手工创建一个示例幻灯片,以设计您希望最终幻灯片的外观,并反向编写生成该幻灯片的代码。经常在幻灯片上用变量字符串替换所有文本,如
{{project_1}}:{{{status_1}}}
将非常有效

问题2:我不知道如何在请求中正确使用objectid 您可以尝试为占位符提供一个对象ID,然后使用InsertText设置内容。我认为您的问题在于占位符IDMappings不正确,您需要一个映射对象列表,而不是多次指定占位符IDMappings键(请参见此)

问题3:是否可以不用写12行来填充占位符? 我认为这主要是由于您如何构建项目数据、幻灯片的布局以及占位符映射问题。解决这些问题应该会让这件事更容易处理

还应该更容易处理SlideApp(使用)