有没有一种方法可以一次性取消链接Google幻灯片演示文稿中的所有图表?

时间:2019-04-18 06:32:30

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

我正在尝试通过使用Slides API的AppScript完成此操作。但是,我是JS菜鸟。请帮忙。

function onOpen() {
    var ui = SlidesApp.getUi();
    ui.createMenu('Custom Menu')
        .addItem('Batch Update Charts', 'batchUpdate')
        .addToUi();
}

function batchUpdate(){
    var gotSlides = SlidesApp.getActivePresentation().getSlides();

    for (var i = 0; i < gotSlides.ID; i++) {
        var slide = gotSlides[i];
        var sheetsCharts = slide.getChartId();
        for (var k = 0; k < sheetsCharts.ID; k++) {
            var shChart = sheetsCharts[k];
            var requests = [{
                createSheetsChart: {
                    chartId: shChart,
                    linkingMode: 'NOT_LINKED_IMAGE',
                }
            }];
            shChart.refresh()
            requests: requests       
        }
    }
}

此代码完全不正确。我希望这会给您一些有关我要实现的目标的提示。

1 个答案:

答案 0 :(得分:0)

如果您是指工作表图表中的URL /索引链接,而不是它中的链接图表属性:

每张幻灯片中的每个removeLink()都可以使用SheetsChart。请参阅https://developers.google.com/apps-script/reference/slides/sheets-chart#removeLink()

function removeAllSheetsChartLinks() {
  SlidesApp.openById(SLIDE_ID).getSlides()
  .forEach(function(s) {
    s.getSheetsCharts().forEach(function(c) {
      if (c.getLink() != null) c.removeLink();
    });
  });
}

但是,如果要删除链接图表属性,则必须删除要从中删除链接图表属性的当前工作表图表,并创建一个具有与原始工作表图表相同的属性的新工作表图表。 linkingMode: 'NOT_LINKED_IMAGE'属性。这是因为没有此类功能的直接Slides API请求。 https://developers.google.com/slides/reference/rest/v1/presentations/request#LinkingMode

function removeLinkChartExample() {

  var presentation = SlidesApp.openById(SLIDE_ID);
  //if empty
  if (presentation == null) throw new Error('Presentation was not found');
  Logger.log("%s id = %s", presentation.getName(), presentation.getId());
  
  var slides = presentation.getSlides();
  if (slides == null) throw new Error('Slides were not found');
  Logger.log("Total of slides in %s: %d", presentation.getName(), slides.length);


  //refresh all linked sheet charts
  slides.forEach(function(s) {
    s.getSheetsCharts().forEach(function(c) {
      //if (c.getLink() != null) 
      { Logger.log("LINKED CHART TITLE: %s, \"%s\"",c.getTitle(), c.getSpreadsheetId()); c.refresh(); } 
    });
  });
  
  var chartToRemoveLinkedChart = slides[0]; //sheet that you want to remove its linked chart property
  if (chartToRemoveLinkedChart == null) return;
  
  var presentationId = presentation.getId();
  
  var sheetObjectId = chartToRemoveLinkedChart.getObjectId();
  Logger.log("Sheet ObjectId of \"%s\": %s", chartToRemoveLinkedChart.getSheetsCharts()[0].getTitle(), sheetObjectId);
  var presentationStaffChartId = chartToRemoveLinkedChart.getSheetsCharts()[0].getObjectId();
  Logger.log("Linked Chart ObjectId of \"%s\": %s", chartToRemoveLinkedChart.getSheetsCharts()[0].getTitle(), presentationStaffChartId);
  var spreadsheetId = chartToRemoveLinkedChart.getSheetsCharts()[0].getSpreadsheetId();
  Logger.log("SS Id of \"%s\": %s", chartToRemoveLinkedChart.getSheetsCharts()[0].getTitle(), spreadsheetId);
  var chartId = chartToRemoveLinkedChart.getSheetsCharts()[0].getChartId();
  Logger.log("SS Chart Id of \"%s\": %s", chartToRemoveLinkedChart.getSheetsCharts()[0].getTitle(), chartId);
  var width = chartToRemoveLinkedChart.getSheetsCharts()[0].getWidth();
  Logger.log("Chart width of \"%s\": %s", chartToRemoveLinkedChart.getSheetsCharts()[0].getTitle(), width);
  var height = chartToRemoveLinkedChart.getSheetsCharts()[0].getHeight();
  Logger.log("Chart height of \"%s\": %s", chartToRemoveLinkedChart.getSheetsCharts()[0].getTitle(), height);
  var top = chartToRemoveLinkedChart.getSheetsCharts()[0].getTop();
  Logger.log("Chart top of \"%s\": %s", chartToRemoveLinkedChart.getSheetsCharts()[0].getTitle(), top);
  var left = chartToRemoveLinkedChart.getSheetsCharts()[0].getLeft();
  Logger.log("Chart left of \"%s\": %s", chartToRemoveLinkedChart.getSheetsCharts()[0].getTitle(), left);

  //remove the linked chart from this sheet before creating a new one
  chartToRemoveLinkedChart.getSheetsCharts()[0].remove();

  presentationStaffChartId = "ga66641c4d9_0_11"; //you need to give your own unique id
  
  var requests = [{
    "createSheetsChart": {
      "objectId": presentationStaffChartId,
      "spreadsheetId": spreadsheetId,
      "chartId": chartId,
      "linkingMode": "NOT_LINKED_IMAGE",
      "elementProperties": {
        "pageObjectId": sheetObjectId,
        "size": {
          "width": {
            "magnitude": width,
            "unit": "PT"
          },
          "height": {
            "magnitude": height,
            "unit": "PT"
          }
        },
        "transform": {
          "scaleX": 1,
          "scaleY": 1,
          "translateX": left,
          "translateY": top,
          "unit": "PT"
        }
      }
    }
  }];
  
  // Execute the request.
  var batchUpdateResponse = Slides.Presentations.batchUpdate({
    requests: requests
  }, presentationId);
  
}