【问题标题】:Auto update Google Slide embedded image自动更新谷歌幻灯片嵌入图像
【发布时间】:2023-01-26 12:31:53
【问题描述】:

我有一张 Google 幻灯片,其中嵌入了我从公共 URL 获取的图像。我希望该图像每天在特定时间自动更新,而不是我必须每天打开幻灯片并通过 URL 替换图像。

这是我尝试使用的代码(用实际值替换占位符“SLIDE_ID”和“IMAGE_URL”)但它不起作用。

function updateEmbeddedImage() {
  // Get the slide ID and the image URL
  var slideId = "SLIDE_ID";
  var imageUrl = "IMAGE_URL";

  // Open the slide and get the existing image
  var slide = SlidesApp.openById(slideId);
  var existingImage = slide.getImages()[0];
  
  // Replace the existing image with the new image
  var newImage = UrlFetchApp.fetch(imageUrl).getBlob();
  slide.replaceImage(existingImage, newImage);
}

// Set a time-based trigger to run the script every day at a specific time
function setTrigger() {
  ScriptApp.newTrigger("updateEmbeddedImage")
    .timeBased()
    .atHour(9)
    .everyDays(1)
    .create();
}

我收到消息:

错误 异常:未找到 updateEmbeddedImage @ Code.gs:7

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    OP 的触发代码可以重复使用。

    OP 的脚本要求源 url 必须硬编码到脚本中。这既不方便又容易出错。

    • 此脚本应绑定到电子表格。
    • 应在 B 列中输入源 URL。
    • A 列可以包含 image() 公式来显示图像(并确认 url 有效)。
    • 脚本获取所有幻灯片,但假定嵌入图像位于第二张幻灯片上。
    • 脚本获取该幻灯片上的所有图像,但假定要替换的图像是幻灯片上的第一张图像。
    • 现有的 SourceURL 返回 .getSourceUrl()
    • 从电子表格中获取新的 SourceURL
      • 存在错误检查以确保可以找到现有的 SourceURL 并且它不是电子表格中的最后一个 URL。

    // this script is bound to the spreadsheet containing the urls of the images.
    // URL are in column B, beginning in row 1
    // Image() formula is in Column A to display the image associated with the url
    // to automatically update every day at a specific time, create a Time driven Installatble trigger.
    function replaceurl() {
    
      // get the slide
      var slideId = "<<insert ID>>"
      var slide = SlidesApp.openById(slideId)
    
      // get the slides; assume the embedded image is on the second slide
      var slides = slide.getSlides()
      var targetSlide = slides[1]
    
      // get images
      // assume that image to replace is the first image on the slide
      var images = targetSlide.getImages()
    
      // get the source url of the existing image
      var existingSourceURL = images[0].getSourceUrl()
      Logger.log("DEBUG: Existing Source URL: "+existingSourceURL)
    
      // get the source url of the next image in the spreadsheet
      // slide urls are in column B
      var ss = SpreadsheetApp.getActiveSpreadsheet()
      var sheet = ss.getSheetByName("SlideDetails")
      var urlRange = sheet.getRange(1,2,sheet.getLastRow())
      Logger.log("DEBUG: URL Range: "+urlRange.getA1Notation())
      // get the values and flatten to return a 1D array
      var urlValues = urlRange.getValues().flat()
      Logger.log(urlValues) //DEBUG
      // get the number of URLs on the spreadsheet
      var valCount = urlValues.length
      Logger.log("DEBUG: # of values in the sheet: "+valCount)
    
      // find the index of the existing source url (zero-based)
      var existingIdx = urlValues.indexOf(existingSourceURL)
      Logger.log("DEBUG: Index of existing url: "+existingIdx);
    
      // test if idx of existing url wasn't found AND
      // test if idx of existing url = num of urls
      // indexOf returns -1 if idx is not found
      if (existingIdx != -1 && existingIdx != (valCount-1)){
        // existing Source URL was found and the idx isn't greater than number of urls on the list
        // new Source URL is next on the list
        var newSourceURL = urlValues[existingIdx+1]
        Logger.log("DEBUG: new source url: "+newSourceURL)
    
        // replace the source URL
        images[0].replace(newSourceURL)
      }
      else{
        // don't change or replace the url
        // don't replace the url
        Logger.log("DEBUG: Image not changed because existing URL is either not found OR was last in the list")
      }
      Logger.log("DEBUG: Done")
    } 
    

    电子表格布局

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      相关资源
      最近更新 更多