我想知道是否有辦法縮短這段代碼。我需要用短語的超鏈接版本查找并替換一堆短語,我希望有一種更有效的方法來做到這一點。先感謝您!!
function insertUrl() {
let ranges = SpreadsheetApp.getActive()
.createTextFinder("Example 1")
.matchEntireCell(true)
.matchCase(true)
.matchFormulaText(false)
.ignoreDiacritics(true)
.findAll();
ranges.forEach(function(range){
range.setFormula('=HYPERLINK("https://www.website1.com/","Example 1")');
}
);
{
let ranges = SpreadsheetApp.getActive()
.createTextFinder("Example 1")
.matchEntireCell(true)
.matchCase(true)
.matchFormulaText(false)
.ignoreDiacritics(true)
.findAll();
ranges.forEach(function(range){
range.setFormula('=HYPERLINK("https://www.website2.com/","Example 1")');
}
);
}
{
let ranges = SpreadsheetApp.getActive()
.createTextFinder("Example 3")
.matchEntireCell(true)
.matchCase(true)
.matchFormulaText(false)
.ignoreDiacritics(true)
.findAll();
ranges.forEach(function(range){
range.setFormula('=HYPERLINK("https://www.website3.com/","Example 3")');
}
);
}
}
uj5u.com熱心網友回復:
創建一個函式,將 URL 和要查找的文本作為引數。
function insertUrl(text, url) {
SpreadsheetApp.getActive()
.createTextFinder(text)
.matchEntireCell(true)
.matchCase(true)
.matchFormulaText(false)
.ignoreDiacritics(true)
.findAll()
.forEach(function (range) {
range.setFormula('=HYPERLINK(' url ',"' text '")');
});
}
insertUrl("Example 1", "https://www.website1.com/");
insertUrl("Example 1", "https://www.website2.com/");
insertUrl("Example 3", "https://www.website3.com/");
你用Example 1錯字website2.com了嗎?你可能打算用Example 2那里代替。
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想降低腳本的處理成本。
在您的情況下,如何進行以下修改?
修改后的腳本:
請確認 的值ar。如果您的實際情況與樣本值不同,請修改。
function insertUrl() {
// Please set the text and the formula you want to use.
const ar = [
{ text: "Example 1", formula: '=HYPERLINK("https://www.website1.com/","Example 1")' },
{ text: "Example 1", formula: '=HYPERLINK("https://www.website2.com/","Example 1")' },
{ text: "Example 3", formula: '=HYPERLINK("https://www.website3.com/","Example 3")' },
];
const ss = SpreadsheetApp.getActive();
ar.forEach(({ text, formula }) =>
ss.createTextFinder(text)
.matchEntireCell(true)
.matchCase(true)
.matchFormulaText(false)
.ignoreDiacritics(true)
.replaceAllWith(formula)
);
}
在此修改中,將創建一個包含您要替換的值的陣列。并且,公式使用
replaceAllWith. 這樣一來,我認為可以減少一點工藝成本。作為降低腳本處理成本的另一個方向,您還可以使用 Sheets API,如下所示。在這種情況下,請在 Advanced Google services 中啟用 Sheets API。
function insertUrl() { // Please set the text and the formula you want to use. const ar = [ { text: "Example 1", formula: '=HYPERLINK("https://www.website1.com/","Example 1")' }, { text: "Example 1", formula: '=HYPERLINK("https://www.website2.com/","Example 1")' }, { text: "Example 3", formula: '=HYPERLINK("https://www.website3.com/","Example 3")' }, ]; const requests = ar.map(({ text, formula }) => ({ findReplace: { allSheets: true, find: text, replacement: formula, matchEntireCell: true, matchCase: true } })); Sheets.Spreadsheets.batchUpdate({ requests }, SpreadsheetApp.getActive().getId()); }
參考:
- replaceAllWith(替換文本)
uj5u.com熱心網友回復:
嘗試使用文字符號
`=HYPERLINK("${url}","${text}")`
腳本
function insertURL() {
const changes = {
'qwant': "https://www.qwant.com/",
'google': "https://www.google.com/",
};
const ss = SpreadsheetApp.getActiveSpreadsheet();
const requests = Object.entries(changes).map(([text, url]) => ({
findReplace: {
find: text,
replacement: `=HYPERLINK("${url}","${text}")`,
matchEntireCell: true,
allSheets: true,
},
}));
Sheets.Spreadsheets.batchUpdate({ requests: requests }, ss.getId());
}
啟用google sheet api服務
批量更新
模板文字
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/432777.html
標籤:javascript 擅长 谷歌应用脚本 谷歌表格
下一篇:谷歌表單預訂
