Estoy confundido en cuanto a por qué mi función se vuelve en blanco cuando intento recorrer varias cuando lo mismo no sucede cuando se hace una. Esta es mi hoja https://docs.google.com/spreadsheets/d/1D4hhDaQnH--_ZqaGXp7h6HnhTTFYzbM0MeuHsT-ak1w/edit#gid=0 Este es mi código:
//arrays input function testCustom(term, startDate,amount, name, soldBy) { var results = new Array(term.length); //loop through every term number in array for(var f = 0; f < term.length; f++) { //loop through term count for(var i = 0; i < term[f]; i++) { //arrray to store all terms var termTempArray = new Array(term[f]); //add increment to date var newDate = new Date(); newDate = startDate; var thirdDate = new Date(); thirdDate.setMonth(newDate.getMonth()+i); //create a new array full of payment date info var payDate = new Array(4); payDate[0] = name; payDate[1] = thirdDate; payDate[2] = amount; payDate[3] = soldBy; termTempArray[i] = payDate; results[f] = termTempArray; } } return results; }Este código funciona donde el anterior vuelve en blanco
//arrays input function frack(term, startDate,amount, name, soldBy) { var results = new Array(term); //loop through every term number in array //for(var f = 0; f < term.length; f++) //{ //loop through term count for(var i = 0; i < term; i++) { //add increment to date var newDate = new Date(); newDate = startDate; var thirdDate = new Date(); thirdDate.setMonth(newDate.getMonth()+i); //create a new array full of payment date info var payDate = new Array(3); payDate[0] = name; payDate[1] = thirdDate; payDate[2] = amount; payDate[3] = soldBy; //add array to results results[i] = payDate; } //} return results; }En lugar de tener que usar manualmente el segundo bit de código para cada fila de datos, quiero tener algo que itere a través del rango de valores y parece que no puedo hacerlo funcionar. Soy nuevo en javascript y en el script de aplicaciones de Google, pero no en la programación en general.
Necesita manejar arreglos 2D correctamente. Quizás sea más fácil obtener todos los datos como una matriz y llamar a la función de esta manera:
=RepeatByNumberOfTerms(A2:E)
...donde cada fila en A2:E contiene un registro, como:
| Términos | Fecha | Monto | Nombre | Vendido por |
|---|---|---|---|---|
| 6 | 12/05/2022 | 100 | jill smith | Dweezil |
| 2 | 12/05/2021 | 20 | jane jones | Unidad Luna |
Luego puede usar la sintaxis de asignación de desestructuración , como esta:
/** * Repeats name, amount and soldBy, incrementing date by one month each time. * * @param {A2:E} data A range where each row contains term, startDate, amount, name, soldBy. * @customfunction */ function RepeatByNumberOfTerms(data) { 'use strict'; if (!Array.isArray(data) || data[0].length !== 5) { throw new Error('Expected a range with at least one row of five columns.'); } const result = []; data.forEach(row => { const [terms, startDate, amount, name, soldBy] = row; if (!Number(terms)) { return; } if (!startDate.getMonth) { throw new Error(`Expected a date, but '${startDate}' is a ${typeof startDate}.`); } const startMonth = startDate.getMonth(); for (let i = 0; i < Math.min(1000, terms); i++) { const date = new Date(startDate); date.setMonth(startMonth + i); result.push([name, date, amount, soldBy]); } }); return result; }Esto obtiene los siguientes resultados:
| A | B | C | D |
|---|---|---|---|
| jill smith | 12/05/2022 | 100 | Dweezil |
| jill smith | 12/06/2022 | 100 | Dweezil |
| jill smith | 7/12/2022 | 100 | Dweezil |
| jane jones | 12/05/2021 | 20 | Unidad Luna |
| jane jones | 12/06/2021 | 20 | Unidad Luna |