Проблема скрипта Google Apps с использованием getLinkUrl (смещение) - PullRequest
0 голосов
/ 09 сентября 2018

Я пытаюсь понять, как использовать getLinkUrl(offset) в скрипте Google Apps.

Я нашел ссылку здесь - http://googlescriptreference.com/Document/Text/getlinkurloffset/,, но я не могу найти никаких примеров в Интернете.

Этот ГАЗ возвращает Paragraph в логах

var paragraph = tableCell.getChild(0);

Этот ГАЗ возвращает the text string in the paragraph в журналах

var paragraphText = paragraph.getText();

Этот ГАЗ возвращает NULL в журналах

var paragraphText = paragraph.getLinkUrl();

Этот GAS возвращает ошибку (Не удается найти метод getLinkUrl (число).) И ничего в журналах

var paragraphText = paragraph.getLinkUrl(2);

Может кто-нибудь помочь мне разобраться, как пользоваться getLinkUrl(offset)?

Спасибо за вашу помощь!

1 Ответ

0 голосов
/ 09 сентября 2018

Я пытался использовать getLinkUrl(offset) непосредственно для элемента абзаца (не рабочий пример), но мне нужно было использовать метод asText() до getLinkUrl(offset).

НЕ работает

var paragraphText = paragraph.getLinkUrl(2);

Рабочая

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

Документ Google 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 помог мне найти это решение. https://stackoverflow.com/users/7108653/tanaike

...