如何将当前的谷歌文档链接到新的谷歌文档,该文档以光标当前所在行的文本命名?



下面是一个用例示例。

  1. 假设我输入Google Doc #1,然后
  2. 我想在Google Doc #1中创建一个链接到一个新的Google Doc(它还不存在)。
  3. 此外,假设我将这个新的Google Doc(它还不存在)命名为"樱桃派"。
  4. 因此,当我在谷歌文档#1中,我会输入"樱桃馅饼",然后我将运行这个脚本。因此,在Google Doc #1中,光标当前所在的行(包含"Cherry pie")现在将被超链接到名为"Cherry pie."的Google Doc(脚本刚刚创建的)。

当我在Google Doc #1中运行下面的脚本时,它创建了一个新的Google Doc (Google Doc #2)。该脚本使用Google Doc #1中光标当前所在行的文本来命名Google Doc #2。

例如,如果光标在Google Doc #1中的一行,文本是&;Cheese pizza",那么脚本将创建一个名为&;Cheese pizza."的新Google Doc

除了什么脚本目前做的,我想脚本也创建一个超链接从"奶酪披萨"(在光标当前所在的行上)从Google Doc #1到Google Doc #2(脚本将创建的文档),名称为"Cheese pizza.">


function onOpen(e) {
DocumentApp.getUi().createMenu('NewDoc')
.addItem('Create', 'newDocWithSentense')
.addToUi();
}
function newDocWithSentense() {
const doc = DocumentApp.getActiveDocument();
const cursor = doc.getCursor();
const surroundings = cursor.getSurroundingText().getText();
const currentFileFolder = DriveApp.getFileById(doc.getId()).getParents().next()
const newDoc = DocumentApp.create(surroundings).getId()
DriveApp.getFileById(newDoc).moveTo(currentFileFolder)
}

我相信你要找的是Text.setLinkUrl()

由于cursor.getSurroundingText()返回一个Text对象,您可以在现有代码的末尾添加这一行:

cursor.getSurroundingText().setLinkUrl(DriveApp.getFileById(newDoc).getUrl())

最新更新