【问题标题】:Update Image in Google Slides when Image updated in Google Drive在 Google Drive 中更新图像时更新 Google Slides 中的图像
【发布时间】:2018-07-28 19:30:11
【问题描述】:

我的目标是根据文件夹中的图像自动创建和更新 Google 幻灯片演示文稿:

当前工作正常,但我想自动化的工作流程如下:

  1. 图片位于 Google 云端硬盘文件夹中
  2. 我使用文件选择器菜单(插入 --> 图像 --> 驱动器)手动将图像添加到每张幻灯片
  3. 文件夹中的图像由外部脚本定期更新,使用相同的文件名替换以前的图像。图片保留相同的 Google 云端硬盘文件 ID
  4. 幻灯片中的图像会自动更新,在浏览器中重新加载幻灯片演示文稿时可以看到更改

我想达到的目标:

不要使用文件选择器一一创建幻灯片和插入图像,而是使用基于驱动器文件夹中的图像文件创建演示文稿的脚本。

我使用了Tutorial on the Google Developers Pages,并且幻灯片的创建工作正常。但是,与手动插入相比,脚本创建的幻灯片中的图像不会更新。该脚本只获取一次图像,并且似乎使用了“图像的副本”。

我的测试代码是:

var NAME = "Dynamic Image Insert Test";
var presentation = SlidesApp.create(NAME);

function addImageSlide(fileID, index) {
    var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);    
    var imageurl = DriveApp.getFileById(fileID);
    var image = slide.insertImage(imageurl);
    var imgWidth   = image.getWidth();
    var imgHeight  = image.getHeight();
    var pageWidth  = presentation.getPageWidth();
    var pageHeight = presentation.getPageHeight();
    var newX = pageWidth/2.  - imgWidth/2.;
    var newY = pageHeight/2. - imgHeight/2.;
    image.setLeft(newX).setTop(newY);
}

function main() {
    var images = [
        "Insert ID of a File 1 on Google Drive here",
        "Insert ID of a File 2 on Google Drive here"      
    ];
    var [title, subtitle] = presentation.getSlides()[0].getPageElements();
    title.asShape().getText().setText(NAME);
    subtitle.asShape().getText().setText("Insert Files from Drive using File ID");
    images.forEach(addImageSlide);
}

另外在检查幻灯片页面的对象属性时似乎有2个参数:contentUrl和sourceUrl

当使用文件选择器插入图像时,图像似乎使用 sourceUrl,它保持不变,这样当文件在云端硬盘上更新时,图像就会更新。当使用 AppScript(或使用 API - 也经过测试)时,图像似乎使用 contentUrl,它在每次创建图像时生成,因此不引用驱动器上的文件,因此不反映对文件的任何更改驱动器。

页面对象属性的记录器输出:

{
  "pageElements": [
    {
      "transform": {
        "scaleX": 171.123,
        "scaleY": 171.123,
        "unit": "EMU",
        "translateY": 1288327.54,
        "translateX": 2375975
      },
      "objectId": "MyImage_01",
      "image": {
        "sourceUrl": "https://lh3.google.com/u/0/d/This-is-the-static-file-id",
        "contentUrl": "https://lh6.googleusercontent.com/This-is-the-id-created-when-creating-the-image-on-the-slide",
        "imageProperties": {
          "shadow": {
            "color": {
              "rgbColor": {}
            },
            "blurRadius": {
              "unit": "EMU"
            },
            "type": "OUTER",
            "transform": {
              "scaleX": 1,
              "scaleY": 1,
              "unit": "EMU"
            },
            "alpha": 1,
            "propertyState": "NOT_RENDERED",
            "rotateWithShape": false,
            "alignment": "BOTTOM_LEFT"
          },
          "outline": {
            "dashStyle": "SOLID",
            "propertyState": "NOT_RENDERED",
            "weight": {
              "unit": "EMU",
              "magnitude": 9525
            },
            "outlineFill": {
              "solidFill": {
                "color": {
                  "themeColor": "DARK2"
                },
                "alpha": 1
              }
            }
          }
        }
      },
      "size": {
        "width": {
          "unit": "EMU",
          "magnitude": 23375
        },
        "height": {
          "unit": "EMU",
          "magnitude": 15000
        }
      }
    }
  ]
}

reference of the API 中也没有记录 sourceUrl。

知道如何在插入幻灯片时使用 sourceUrl 而不是 contentUrl 吗?如何使用 Google 应用程序脚本复制与文件选择器相同的插入语句和文件引用?

【问题讨论】:

  • 您好,您找到解决此问题的可接受方法了吗?我遇到了同样的情况;但我已准备好单击“按钮”以启动幻灯片的更新过程。谢谢

标签: google-apps-script google-drive-api google-slides-api google-slides


【解决方案1】:

我已使用此脚本更新一张幻灯片上的所有图像。我目前正在考虑在用户通过 Google 表单加载图像时自动执行此功能。也许这可以帮助您解决问题。

/////////////////////////////////////////

var deck = SlidesApp.getActivePresentation()
var slide = deck.appendSlide(SlidesApp.PredefinedLayout.BLANK)
function main() {
var folder = DriveApp.getFolderById('your folder id goes here'); // Change the folder  
ID  here
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
var imageUrl = DriveApp.getFileById(file.getId()).getDownloadUrl() + "&access_token=" 
+ ScriptApp.getOAuthToken();
Logger.log(DriveApp.getFileById(file.getId()).getDownloadUrl() + "&access_token=" + 
ScriptApp.getOAuthToken()); 
var image = slide.insertImage(imageUrl);
}}
////////////////////////////////////

问候 卢克

【讨论】:

    【解决方案2】:

    所以我做的另一件事是使用“.replace”将 blob 替换为新 URL。

        function rePlace(){
        var IdBox = SlidesApp.getActivePresentation().getSlides()[0].getImages();
        //var L_nk = IdBox[1].
        IdBox[1].select();         
        var userProperties = PropertiesService.getUserProperties();
        var loopCounter = Number(userProperties.getProperty('loopCounter'));       
        IdBox[1].replace("your URL")
        } 
    

    【讨论】:

      【解决方案3】:

      我发现(在 Python 中)根据驱动器中更新源图像的 SAME source_url 进行 content_url 更新的唯一方法是使用 access_token 重新生成 source_url

      img_url = '%s&access_token=%s' % (
          DRIVE.files().get_media(fileId=rsp['id']).uri, creds.access_token) 
      

      (http://wescpy.blogspot.com/search/label/API)

      阅读博客了解更多详情。

      一旦你设法获得新的 url,你可以用替换图片请求来更新它

      https://developers.google.com/slides/reference/rest/v1/presentations/request#ReplaceImageRequest

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多