Buenas tardes,
Actualmente estoy desarrollando una base de datos en las hojas de Google que estoy tratando de llenar con datos de otro GSheet usando un script que actúa como un VLookup
Usé el script de este comentario: https://stackoverflow.com/a/60266697/19427703
El problema al que me enfrento es que cuando ejecuto mi script, se borran todos los datos (incluso cuando los datos no coinciden). Me gustaría que mi secuencia de comandos deje los datos en la celda cuando la ID no coincida.
Apreciaría cualquier ayuda,
Gracias de antemano, aquí está mi código:
const ss = SpreadsheetApp.getActive(); /** * @param {GoogleAppsScript.Spreadsheet.Sheet} fromSht -Sheet to import from * @param {GoogleAppsScript.Spreadsheet.Sheet} toSht -Sheet to import to * @param {Number} fromCompCol -Column number of fromSht to compare * @param {Number} toCompCol -Column number of toSht to compare * @param {Number} fromCol -Column number of fromSht to get result * @param {Number} toCol -Column number of toSht to get result */ function Refresh( fromSht = ss.getSheetByName('Sheet1'), toSht = ss.getSheetByName('Sheet2'), fromCompCol = 1, toCompCol = 7, fromCol = 2, toCol = 2 ) { const toShtLr = toSht.getLastRow(); const toCompArr = toSht.getRange(2, toCompCol, toShtLr - 1, 1).getValues(); const fromArr = fromSht.getDataRange().getValues(); fromCompCol--; fromCol--; /*Create a hash object of fromSheet*/ const obj1 = fromArr.reduce((obj, row) => { let el = row[fromCompCol]; el in obj ? null : (obj[el] = row[fromCol]); return obj; }, {}); //Paste to column toSht .getRange(2, toCol, toShtLr - 1, 1) .setValues(toCompArr.map(row => (row[0] in obj1 ? [obj1[row[0]]] : [null]))); }null se establece aquí, si la row[0] no está en el objeto hash:
setValues(toCompArr.map(row => (row[0] in obj1 ? [obj1[row[0]]] : [null]))); }
Probar
setValues(toCompArr.map(row => (row[0] in obj1 ? [obj1[row[0]]] : row)));