Objetivo: apilar datos de más de 90 libros de trabajo de Google, todos con el mismo nombre de hoja, en una hoja maestra para generar informes
Información: Todas las hojas de cálculo tienen el mismo número de columnas. Tengo el siguiente script pero no se ejecuta correctamente, creo que el problema es cómo estoy almacenando en caché/empujando los datos a la matriz antes de pegarlos en la hoja de salida.
Estoy tratando de construir una matriz y luego pegarla de una sola vez.
Las tablas que estoy apilando tienen 47 columnas, número desconocido de filas. La parte que abre las hojas funciona perfectamente.
// Get the data from the worksheets var indexsheet = SpreadsheetApp.getActive().getSheetByName("Index"); var outputsheet = SpreadsheetApp.getActive().getSheetByName("Output"); var response = SpreadsheetApp.getUi().prompt('Current Cycle', 'Enter Cycle Name Exactly in YY-MMM-Cycle# format', SpreadsheetApp.getUi().ButtonSet.OK_CANCEL) var CurrentCycleName = response.getResponseText() // Assign datasets to variables var indexdata = indexsheet.getDataRange().getValues(); // For each workbook in the index sheet, open it and copy the data to a cache indexdata.forEach(function(row, r) { try { //open Entity specific workbook var workbookid = indexsheet.getRange(r + 1, 7, 1, 1).getValues(); var Entityworkbook = SpreadsheetApp.openById(workbookid) // Open workhseet Entitysheet.getSheetByName(CurrentCycleName) // Add PR Data to cache - stacking for all countrys var PRDataCache = Entitysheet.getDataRange().push() } catch {} }) // Set the all values of the sheet at once outputsheet.getRange(r + 1, 14).setValue('Issue Splitting Data') Entitysheet.getRange(2, 1, PRDataCache.length || 1, 47).setValues(PRDataCache) };
Esta es la pestaña de índice de donde obtenemos el workbookid para abrir cada archivo
Este es el archivo de salida, estamos apilando todos los datos de cada país
Creo que su objetivo es el siguiente.
En este caso, ¿qué tal el siguiente script de muestra?
function myFunction() { var ss = SpreadsheetApp.getActive(); var indexsheet = ss.getSheetByName("Index"); var outputsheet = ss.getSheetByName("Output"); var response = SpreadsheetApp.getUi().prompt('Current Cycle', 'Enter Cycle Name Exactly in YY-MMM-Cycle# format', SpreadsheetApp.getUi().ButtonSet.OK_CANCEL); var CurrentCycleName = response.getResponseText(); var ids = indexsheet.getRange("G1:G" + indexsheet.getLastRow()).getValues(); var values = ids.reduce((ar, [id]) => { try { var [, ...values] = SpreadsheetApp.openById(id).getSheetByName(CurrentCycleName).getDataRange().getValues(); ar = [...ar, ...values]; } catch (e) { console.log(`"${id}" was not found.`); } return ar; }, []); if (values.length == 0) return; // If the number of columns is different in all Spreadsheets, please use the following script. // var maxLen = Math.max(...values.map(r => r.length)); // values = values.map(r => r.length < maxLen ? [...r, ...Array(maxLen - r.length).fill("")] : r); outputsheet.getRange(outputsheet.getLastRow() + 1, 1, values.length, values[1].length).setValues(values); }