Me pregunto si hay alguna forma de acortar este código. Necesito encontrar y reemplazar un montón de frases con la versión hipervinculada de la frase, y espero que haya una forma más eficiente de hacerlo. ¡¡Gracias de antemano!!
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")'); } ); } }Cree una función que tome la URL y el texto para buscar como parámetros.
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/"); ¿Tu uso del Example 1 con website2.com fue un error tipográfico? Probablemente quisiste usar el Example 2 allí en su lugar.
Creo que su objetivo es el siguiente.
En su situación, ¿qué tal la siguiente modificación?
Confirme el valor de ar . Si su situación real es diferente del valor de la muestra, modifíquelos.
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) ); } En esta modificación, se crea una matriz que incluye los valores que desea reemplazar. Y, la fórmula se pone usando replaceAllWith . Por esto, pensé que el costo del proceso podría reducirse un poco.
Como otra dirección para reducir el costo del proceso de su secuencia de comandos, también puede usar Sheets API de la siguiente manera. En este caso, habilite Sheets API en los servicios avanzados de Google .
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()); }Pruebe y use la notación literal
`=HYPERLINK("${url}","${text}")`guion
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()); }habilitar el servicio api de hojas de google