Google 用于在 Google 幻灯片中创建 link 的应用脚本

Google App Script to create link in Google Slides

我正在从 Google Sheet 中提取数据以从模板创建新的 Google 幻灯片。一切正常,但我无法弄清楚如何在用电子表格数据替换模板变量后使文本可点击 link。

如何用网站 link 替换我的网站变量 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

}

我相信你的目标如下。

  • 您想在脚本中将 hyperlink 赋予 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。届时,replaceAllText的返回值作为replace的校验。

参考文献:

添加:第二个问题的答案

关于您的新问题 Does setLinkUrl(startOffset, endOffsetInclusive, url) not work in this context?,我认为幻灯片服务中没有 setLinkUrl(startOffset, endOffsetInclusive, url) 的方法。我想这可能是文档服务的方法。 Ref所以这个不能直接用。如果要将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.getRange(startOffset, endOffset).getTextStyle().setLinkUrl(company_website);  // Modified
});
  • 在这种情况下,请设置startOffset, endOffset

参考: