首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用appscript从google工作表中删除google幻灯片

用appscript从google工作表中删除google幻灯片
EN

Stack Overflow用户
提问于 2022-09-22 11:24:41
回答 1查看 38关注 0票数 0

我正在用我找到的代码更新google幻灯片中的内容,这一切都很好。但是,当我运行脚本时,我希望在第n张幻灯片之后的当前幻灯片在添加新幻灯片之前被删除。甚至有可能吗?

谷歌单张 谷歌幻灯片平台

代码语言:javascript
运行
复制
function createOneSlidePerRow() {

  // Replace <INSERT_SLIDE_DECK_ID> wih the ID of your 
  // Google Slides presentation.
  let masterDeckID = "INSERT_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[2];

  // Load data from the spreadsheet.
  let dataRange = SpreadsheetApp.getActive().getSheetByName('Sheet2').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("{{test1}}", row[0]);
    slide.replaceAllText("{{test2}}", row[1]);
    slide.replaceAllText("{{test3}}", 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();

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-22 12:22:09

解决方案:

使用Slide.remove()

代码片段:

代码语言:javascript
运行
复制
const nIndex = "nth index from which slides get removed";
const deck = SlidesApp.openById(SLIDE_ID);
const slides = deck.getSlides();
slides.slice(nIndex).forEach(s => s.remove());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73813755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档