如何根据值自动将 Google 表格中的列值转换为超链接?
How can I automatically turn a column value in Google Sheets into a hyperlink based on the value?
我有一个 sheet,其中一列包含 Jira 票证的 ID。
我想根据我输入的值自动将其转换为 link 机票。
例如我将在该列中输入 SD-1234,然后我希望它变成可点击的 link 到 https://demo.atlassian.net/browse/SD-1234/,但不显示单元格中的 URL,但是我输入的原始值 (SD-1234).
如果有帮助,该列始终为 E。有人可以告诉我如何在脚本编辑器中编写脚本吗?
例如,当单元格“E1”有SD-1234
时,使用=HYPERLINK("https://demo.atlassian.net/browse/"&E1, E1)
,则值为SD-1234
,超链接为https://demo.atlassian.net/browse/SD-1234
。但在这种情况下,结果不能直接显示在“E1”单元格中。当您想直接将单元格“E1”中的SD-1234
转换为带有https://demo.atlassian.net/browse/SD-1234
超链接的SD-1234
时,下面的示例脚本如何?
示例脚本:
请复制并粘贴以下脚本和 运行 脚本编辑器中的函数。在使用之前,请设置 sheet 名称。
此示例脚本将“E”列中的 SD-1234
转换为 =HYPERLINK("https://demo.atlassian.net/browse/SD-1234", "SD-1234")
。
function sample1() {
const sheetName = "Sheet1"; // Please set the sheet name.
const baseUrl = "https://demo.atlassian.net/browse/";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const range = sheet.getRange("E1:E" + sheet.getLastRow());
const values = range.getValues().map(([e]) => [`=HYPERLINK("${baseUrl}${e}", "${e}")`]);
range.setFormulas(values);
}
此示例脚本将“E”列中的 SD-1234
转换为具有超链接 https://demo.atlassian.net/browse/SD-1234
的 SD-1234
。在这种情况下,不使用公式。
function sample2() {
const sheetName = "Sheet1"; // Please set the sheet name.
const baseUrl = "https://demo.atlassian.net/browse/";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const range = sheet.getRange("E1:E" + sheet.getLastRow());
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
}
注:
- 如果您的点差sheet有第一行header,请将
"E1:E" + sheet.getLastRow()
修改为"E2:E" + sheet.getLastRow()
。
参考文献:
已添加 1 个:
根据您的以下评论,
Do I have to set the sheet name? I have several sheets that I always want this function to be applied to (I have currently over 20 sheets in the file).
Also, I've entered this into Script Editor to test, and what do I do from here? There's no save that I can see... and it's not running on my sheet. How can I apply this to my sheet to automatically run whenever a value is entered in the cell?
当以上情况反映到脚本中,就变成了下面的样子。
示例脚本:
请将以下脚本复制并粘贴到脚本编辑器中并保存。并请编辑 sheet 的“E”列。这样,单元格就有了输入文本的超链接。
function onEdit(e) {
const sheetNames = ["Sheet1", "Sheet2",,,]; // Please set the sheet names you want to run the script.
const column = 5; // Column E. From your question, this script run for the column "E".
const {range} = e;
const sheet = range.getSheet();
const baseUrl = "https://demo.atlassian.net/browse/";
if (!sheetNames.includes(sheet.getSheetName()) || range.columnStart != column) return;
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
}
如果你想运行所有sheet的脚本,你可以使用下面的脚本。
function onEdit(e) {
const column = 5; // Column E. From your question, this script run for the column "E".
const {range} = e;
const sheet = range.getSheet();
const baseUrl = "https://demo.atlassian.net/browse/";
if (range.columnStart != column) return;
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
}
添加了 2 个:
如果你想运行通过一个脚本运行ning将所有sheet的列“E”脚本运行,你也可以使用下面的脚本。
示例脚本:
此示例脚本 运行s 用于特定的 sheets。
function myFunction() {
const sheetNames = ["Sheet1", "Sheet2",,,]; // Please set sheet names you want to run the script.
const baseUrl = "https://demo.atlassian.net/browse/";
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(sheet => {
if (!sheetNames.includes(sheet.getSheetName())) return;
const range = sheet.getRange("E1:E" + sheet.getLastRow());
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
});
}
此示例脚本 运行 用于所有 sheet。
function myFunction() {
const baseUrl = "https://demo.atlassian.net/browse/";
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(sheet => {
const range = sheet.getRange("E1:E" + sheet.getLastRow());
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
});
}
我有一个 sheet,其中一列包含 Jira 票证的 ID。
我想根据我输入的值自动将其转换为 link 机票。
例如我将在该列中输入 SD-1234,然后我希望它变成可点击的 link 到 https://demo.atlassian.net/browse/SD-1234/,但不显示单元格中的 URL,但是我输入的原始值 (SD-1234).
如果有帮助,该列始终为 E。有人可以告诉我如何在脚本编辑器中编写脚本吗?
例如,当单元格“E1”有SD-1234
时,使用=HYPERLINK("https://demo.atlassian.net/browse/"&E1, E1)
,则值为SD-1234
,超链接为https://demo.atlassian.net/browse/SD-1234
。但在这种情况下,结果不能直接显示在“E1”单元格中。当您想直接将单元格“E1”中的SD-1234
转换为带有https://demo.atlassian.net/browse/SD-1234
超链接的SD-1234
时,下面的示例脚本如何?
示例脚本:
请复制并粘贴以下脚本和 运行 脚本编辑器中的函数。在使用之前,请设置 sheet 名称。
此示例脚本将“E”列中的 SD-1234
转换为 =HYPERLINK("https://demo.atlassian.net/browse/SD-1234", "SD-1234")
。
function sample1() {
const sheetName = "Sheet1"; // Please set the sheet name.
const baseUrl = "https://demo.atlassian.net/browse/";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const range = sheet.getRange("E1:E" + sheet.getLastRow());
const values = range.getValues().map(([e]) => [`=HYPERLINK("${baseUrl}${e}", "${e}")`]);
range.setFormulas(values);
}
此示例脚本将“E”列中的 SD-1234
转换为具有超链接 https://demo.atlassian.net/browse/SD-1234
的 SD-1234
。在这种情况下,不使用公式。
function sample2() {
const sheetName = "Sheet1"; // Please set the sheet name.
const baseUrl = "https://demo.atlassian.net/browse/";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const range = sheet.getRange("E1:E" + sheet.getLastRow());
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
}
注:
- 如果您的点差sheet有第一行header,请将
"E1:E" + sheet.getLastRow()
修改为"E2:E" + sheet.getLastRow()
。
参考文献:
已添加 1 个:
根据您的以下评论,
Do I have to set the sheet name? I have several sheets that I always want this function to be applied to (I have currently over 20 sheets in the file).
Also, I've entered this into Script Editor to test, and what do I do from here? There's no save that I can see... and it's not running on my sheet. How can I apply this to my sheet to automatically run whenever a value is entered in the cell?
当以上情况反映到脚本中,就变成了下面的样子。
示例脚本:
请将以下脚本复制并粘贴到脚本编辑器中并保存。并请编辑 sheet 的“E”列。这样,单元格就有了输入文本的超链接。
function onEdit(e) {
const sheetNames = ["Sheet1", "Sheet2",,,]; // Please set the sheet names you want to run the script.
const column = 5; // Column E. From your question, this script run for the column "E".
const {range} = e;
const sheet = range.getSheet();
const baseUrl = "https://demo.atlassian.net/browse/";
if (!sheetNames.includes(sheet.getSheetName()) || range.columnStart != column) return;
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
}
如果你想运行所有sheet的脚本,你可以使用下面的脚本。
function onEdit(e) {
const column = 5; // Column E. From your question, this script run for the column "E".
const {range} = e;
const sheet = range.getSheet();
const baseUrl = "https://demo.atlassian.net/browse/";
if (range.columnStart != column) return;
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
}
添加了 2 个:
如果你想运行通过一个脚本运行ning将所有sheet的列“E”脚本运行,你也可以使用下面的脚本。
示例脚本:
此示例脚本 运行s 用于特定的 sheets。
function myFunction() {
const sheetNames = ["Sheet1", "Sheet2",,,]; // Please set sheet names you want to run the script.
const baseUrl = "https://demo.atlassian.net/browse/";
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(sheet => {
if (!sheetNames.includes(sheet.getSheetName())) return;
const range = sheet.getRange("E1:E" + sheet.getLastRow());
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
});
}
此示例脚本 运行 用于所有 sheet。
function myFunction() {
const baseUrl = "https://demo.atlassian.net/browse/";
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(sheet => {
const range = sheet.getRange("E1:E" + sheet.getLastRow());
const values = range.getValues().map(([e]) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(baseUrl + e).build()]);
range.setRichTextValues(values);
});
}