javascript - 使用 getLinkUrl(offset) 的 Google Apps 脚本问题
问题描述
我试图弄清楚如何getLinkUrl(offset)
在 Google Apps Script 中使用。
我在这里找到了参考 - http://googlescriptreference.com/Document/Text/getlinkurloffset/,但我在网上找不到任何示例。
此 GASParagraph
在日志中返回
var paragraph = tableCell.getChild(0);
此 GASthe text string in the paragraph
在日志中返回
var paragraphText = paragraph.getText();
此 GASNULL
在日志中返回
var paragraphText = paragraph.getLinkUrl();
此 GAS 返回错误(找不到方法 getLinkUrl(number)。)并且日志中没有任何内容
var paragraphText = paragraph.getLinkUrl(2);
有人可以帮我了解如何使用getLinkUrl(offset)
吗?
谢谢你的帮助!
解决方案
我试图getLinkUrl(offset)
直接在段落元素上使用(不是工作示例),但我需要使用asText()
之前的方法getLinkUrl(offset)
。
不工作
var paragraphText = paragraph.getLinkUrl(2);
在职的
var paragraphText = paragraph.asText().getLinkUrl(2);
谷歌文档 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
推荐阅读
- javascript - 状态突变是副作用?
- tensorflow - 从 Java 中使用 DNNRegressor 生成的模型中读取权重
- c - socket() 中“type”为 SOCK_DGRAM 或 SOCK_STREAM 时是否需要指定“protocol”?
- django - Google OAuth 2.0 不断尝试使用“http”url 进行回调
- pandas - 比较特定列的值并将值添加到另一个 | 熊猫 | Python
- c# - 使用 EF Core 进行条件选择
- mongodb - MongoDB ChangeStream ResumeToken 是全球唯一的吗?
- python - 如何通过阻止输入的最新字符来限制 python 中 tkinter 文本小部件的长度?
- javascript - 在javascript中添加两个带有复选框的下拉列表
- machine-learning - 卷积神经网络和多类分类