首页 > 解决方案 > 谷歌应用脚​​本在谷歌幻灯片中创建链接

问题描述

我正在从 Google 表格中提取数据以从模板创建新的 Google 幻灯片。一切正常,但是一旦我用电子表格数据替换了模板变量,我就无法弄清楚如何将文本作为链接点击。

如何用网站链接替换我的网站变量 company_website 并使其可点击?

function createSlidesfromSheets() {
  var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/xxxxxlink/edit"; //make sure this includes the '/edit at the end
  var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
  var deck = SlidesApp.getActivePresentation();
  var sheet = ss.getSheetByName('Sheet1'); // this needs to be the name of the sheet/feuille
  var values = sheet.getRange('A2:M20').getValues(); //this is the range of data to create the slides from. 
  //Logger.log(values);
  var slides = deck.getSlides();
  var templateSlideOne = slides[0];
  var templateSlideTwo = slides[1];
  var presLength = slides.length;
  
  values.forEach(function(page){
  if(page[0]){
    
   var company_name = page[0];
   var company_country = page[1];   
   var company_city = page[2];   
   var company_website = page[3];   
   var company_description = page[4];
   

    
   templateSlideOne.duplicate(); //duplicate the first template slide
   templateSlideTwo.duplicate(); //duplicate the second template slide
   slides = deck.getSlides(); //update the slides array for indexes and length
   
   newSlideOne = slides[1]; // declare the copy to update of the first template slide (right after the first template's slide)
   newSlideTwo = slides[3]; // declare the copy to update of the second template slide (right after the second template's slide)
    
   var firstShapes = (newSlideOne.getShapes());
     firstShapes.forEach(function(shape){
       shape.getText().replaceAllText('{{company-name}}',company_name);
       shape.getText().replaceAllText('{{company-city}}',company_city);       
       shape.getText().replaceAllText('{{company-country}}',company_country);
       shape.getText().replaceAllText('{{company-website}}',company_website);

// how do I use setLinkUrl() to make the new company_website text a clickable link?
       
    }); 
   var secondShapes = (newSlideTwo.getShapes());
    secondShapes.forEach(function(shape){
       shape.getText().replaceAllText('{{company-name}}',company_name);
       shape.getText().replaceAllText('{{company-description}}',company_description);       

    }); 
   presLength = slides.length; 
   newSlideOne.move(presLength);
   presLength = slides.length; 
   newSlideTwo.move(presLength); 
  } // end our conditional statement
  }); //close our loop of values

}

标签: google-apps-scriptgoogle-sheetsgoogle-slides

解决方案


我相信你的目标如下。

  • 您想shape.getText().replaceAllText('{{company-website}}',company_website)在脚本中提供超链接。

为了实现这一点,这个修改怎么样?

从:

     firstShapes.forEach(function(shape){
       shape.getText().replaceAllText('{{company-name}}',company_name);
       shape.getText().replaceAllText('{{company-city}}',company_city);       
       shape.getText().replaceAllText('{{company-country}}',company_country);
       shape.getText().replaceAllText('{{company-website}}',company_website);

// how do I use setLinkUrl() to make the new company_website text a clickable link?
       
    }); 

到:

firstShapes.forEach(function(shape){
  var text = shape.getText();
  text.replaceAllText('{{company-name}}',company_name);
  text.replaceAllText('{{company-city}}',company_city);       
  text.replaceAllText('{{company-country}}',company_country);
  var n = text.replaceAllText('{{company-website}}',company_website);
  if (n > 0) text.getTextStyle().setLinkUrl(company_website);
});
  • 在此修改中,TextStyle 是从 TextRange 中检索并setLinkUrl使用的。此时,from的返回值replaceAllText作为replace的校验。

参考:

补充:回答第二个问题

关于你的新问题Does setLinkUrl(startOffset, endOffsetInclusive, url) not work in this context?,我认为幻灯片服务中没有方法setLinkUrl(startOffset, endOffsetInclusive, url)。我认为这可能是文档服务的方法。Ref所以这不能直接使用。如果您想将链接反映到文本部分,请使用

示例脚本:

firstShapes.forEach(function(shape){
  var text = shape.getText();
  text.replaceAllText('{{company-name}}',company_name);
  text.replaceAllText('{{company-city}}',company_city);       
  text.replaceAllText('{{company-country}}',company_country);
  var n = text.replaceAllText('{{company-website}}',company_website);
  if (n > 0) text.getRange(startOffset, endOffset).getTextStyle().setLinkUrl(company_website);  // Modified
});
  • 在这种情况下,请设置startOffset, endOffset.

参考:


推荐阅读