Google Apps Script problem using getLinkUrl(offset)

安稳与你 提交于 2019-12-11 17:14:52

问题


I'm trying to figure out how to use getLinkUrl(offset) in Google Apps Script.

I found the reference here - http://googlescriptreference.com/Document/Text/getlinkurloffset/, but I can't find any examples online.

This GAS returns Paragraphin the logs

var paragraph = tableCell.getChild(0);

This GAS returns the text string in the paragraph in the logs

var paragraphText = paragraph.getText();

This GAS returns NULL in the logs

var paragraphText = paragraph.getLinkUrl();

This GAS returns an error ( Cannot find method getLinkUrl(number).) and nothing in the logs

var paragraphText = paragraph.getLinkUrl(2);

Can someone please help me understand how to use the getLinkUrl(offset)?

Thanks for your help!


回答1:


I was attempting to use the getLinkUrl(offset) directly on the paragraph element (not working example), but I needed to use asText() method before getLinkUrl(offset).

NOT Working

var paragraphText = paragraph.getLinkUrl(2);

Working

var paragraphText = paragraph.asText().getLinkUrl(2);

Google Document https://docs.google.com/document/d/136G549zIYPYBndXs70ZnR_wEFg5fPST9ZGsOlTgmDyM/edit

//Works if link is on a word or words instead of entire paragraph

function myFunctionP1() {
  //Find out how many Children there are
  var body = DocumentApp.getActiveDocument().getBody();
  Logger.log(body.getNumChildren());

  //Find out which child is table
  var table = body.getChild(2); //table is child#2
  Logger.log(table);

  //Find tableCell inside of table
  var tableCell = table.getChild(0).getChild(0); //tableCell
  Logger.log(tableCell)

  //Find paragraph inside of tableCell 
  //I think, but I'm not sure, the HYPERLINK will be found somewhere within the PARAGRAPH element. But I can't figure out how to get to it.
  var paragraph = tableCell.getChild(0); //paragraph element
  Logger.log(paragraph)

  //Get paragraph text 
  var paragraphText = paragraph.asText().getLinkUrl(2); //paragraph text
  Logger.log(paragraphText)


}



//Works if link is on entire paragraph
//Works if link is on entire paragraph
//Works if link is on entire paragraph
function myFunctionP2() {
  //Find out how many Children there are
  var body = DocumentApp.getActiveDocument().getBody();
  Logger.log(body.getNumChildren());

  //Find out which child is table
  var table = body.getChild(4); //table is child#2
  Logger.log(table);

  //Find tableCell inside of table
  var tableCell = table.getChild(0).getChild(0); //tableCell
  Logger.log(tableCell)

  //Find paragraph inside of tableCell 
  //I think, but I'm not sure, the HYPERLINK will be found somewhere within the PARAGRAPH element. But I can't figure out how to get to it.
  var paragraph = tableCell.getChild(0); //paragraph element
  Logger.log(paragraph)

  //Get paragraph text 
  var paragraphText = paragraph.getText(); //paragraph text
  Logger.log(paragraphText)

  //Get HYPERLIINK from paragraph text 
  var paragraphHYPERLINK = paragraph.getLinkUrl(); //paragraph text
  Logger.log(paragraphHYPERLINK)  

}

@Tanaike helped me find this solution. https://stackoverflow.com/users/7108653/tanaike



来源:https://stackoverflow.com/questions/52240669/google-apps-script-problem-using-getlinkurloffset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!