【问题标题】:Google Slides - add slide with appscript through Sheets new slide to be added in the end of the presentation谷歌幻灯片 - 通过表格添加带有 appscript 的幻灯片,新幻灯片将添加到演示文稿的末尾
【发布时间】:2022-09-23 01:19:32
【问题描述】:

我有这段代码可以将 Google 表格中的内容添加到 Google 幻灯片中,效果很好,但是我希望在演示文稿的末尾添加新幻灯片。我不确定我是否可以在代码let slide = masterSlide.duplicate(); 的这个区域周围的某个地方使用类似appendSlide() 的东西

现在它的行为是这样的...... 标题幻灯片、模板幻灯片、[新幻灯片]、幻灯片 1、幻灯片 2

期望的结果... 标题幻灯片、模板幻灯片、幻灯片 1、幻灯片 2、[新幻灯片]


  // Replace <INSERT_SLIDE_DECK_ID> wih the ID of your 
  // Google Slides presentation.
  let masterDeckID = \"SLIDE_DECK_ID\";

  // Open the presentation and get the slides in it.
  let deck = SlidesApp.openById(masterDeckID);
  let slides = deck.getSlides();

  // The 2nd slide is the template that will be duplicated
  // once per row in the spreadsheet.
  let masterSlide = slides[1];

  // Load data from the spreadsheet.
  let dataRange = SpreadsheetApp.getActive().getSheetByName(\'Sheet1\').getDataRange();
  let sheetContents = dataRange.getValues();

  // Save the header in a variable called header
  let header = sheetContents.shift();

  // Create an array to save the data to be written back to the sheet.
  // We\'ll use this array to save links to the slides that are created.
  let updatedContents = [];

  // Reverse the order of rows because new slides will
  // be inserted at the top. Without this, the order of slides
  // will be the inverse of the ordering of rows in the sheet. 
  sheetContents.reverse();

  // For every row, create a new slide by duplicating the master slide
  // and replace the template variables with data from that row.
  sheetContents.forEach(function (row) {

    // Insert a new slide by duplicating the master slide.
    let slide = masterSlide.duplicate();

    // Populate data in the slide that was created
    slide.replaceAllText(\"{{firstName}}\", row[0]);
    slide.replaceAllText(\"{{lastName}}\", row[1]);
    slide.replaceAllText(\"{{grade}}\", row[2]);

    // Create the URL for the slide using the deck\'s ID and the ID
    // of the slide.
    let slideUrl = `https://docs.google.com/presentation/d/${deck.getId()}/edit#slide=id.${slide.getObjectId()}`;

    // Add this URL to the 4th column of the row and add this row
    // to the data to be written back to the sheet.
    row[3] = slideUrl;
    updatedContents.push(row);
  });

  // Add the header back (remember it was removed using 
  // sheetContents.shift())
  updatedContents.push(header);

  // Reverse the array to preserve the original ordering of 
  // rows in the sheet.
  updatedContents.reverse();

  // Write the updated data back to the Google Sheets spreadsheet.
  dataRange.setValues(updatedContents);

  // Remove the master slide if you no longer need it.
  //masterSlide.remove();

}```

  • 每次代码运行后,您是否删除工作表中的信息?或者当添加新行时,您如何处理幻灯片中的所有重复项?

标签: google-apps-script google-sheets google-slides


【解决方案1】:

我不确定你是如何处理防止重复的。但是,我通过在添加新信息之前删除工作表中的所有信息来进行测试。

以下是在演示文稿末尾添加幻灯片的一些建议:

代替:

  // Open the presentation and get the slides in it.
  let deck = SlidesApp.openById(masterDeckID);
  let slides = deck.getSlides();

  // once per row in the spreadsheet.
  let masterSlide = slides[1];

和:

  let deck = SlidesApp.openById(masterDeckID);
  let masterSlide = deck.getSlides()[1];

还有以下更改,替换:

    let slide = masterSlide.duplicate();

和:

    let slide = deck.appendSlide(masterSlide);

参考:

appendSlide(slide)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 2022-08-21
    • 2018-03-29
    • 2016-08-15
    相关资源
    最近更新 更多