Tengo una hoja llamada "Main_Sheet". Tengo varias pestañas (Este, Central) para cada valor de columna en Col_B. Cada semana se agregan nuevos registros a la hoja principal. Quiero una función de script de aplicaciones de Google para insertar nuevas filas desde la pestaña Main_Sheet a las pestañas correspondientes.
Hoja de entrada:
Hoja_principal
Salida_Hojas Centro Este
Pasos a seguir: Las filas delineadas son nuevas (porque aún no existen en las Hojas Este y Central). Tome estas filas y pegue sus valores en las pestañas correspondientes (Este y Centro)
La hoja de salida debe tener el siguiente aspecto:
Después de ejecutar el script, las filas resaltadas deben agregarse a la pestaña "Este":
Del mismo modo, Central se vería así:
Dado que la pestaña "Oeste" no existe, me gustaría que la secuencia de comandos cree una nueva pestaña e inserte registros
Soy nuevo en las hojas de Google y el script de aplicaciones, por favor ayuda.
En su situación, ¿qué tal el siguiente script de muestra?
function myFunction() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheets = ss.getSheets().reduce((o, s) => (o[s.getSheetName()] = s, o), {}); const main = ss.getSheetByName("Main_Sheet"); const [header, ...values] = main.getDataRange().getValues(); const col = main.getLastColumn(); const obj = values.reduce((o, r) => (o[r[1]] = o[r[1]] ? [...o[r[1]], r] : [r], o), {}); Object.entries(obj).forEach(([s, v]) => { if (v.length == 0) return; if (sheets[s]) { const sheet = sheets[s]; const temp = sheet.getDataRange().getValues().reduce((o, r) => (o[r.join("")] = true, o), {}); const values = v.filter(r => !temp[r.join("")]); if (values.length > 0) { const lastRow = sheet.getLastRow(); sheet.getRange(lastRow + 1, 1, values.length, values[0].length).setValues(values); main.getRange("A2:2").copyFormatToRange(sheet, 1, col, lastRow + 1, lastRow + values.length); } } else { const values = [header, ...v]; const sheet = ss.insertSheet(s); sheet.getRange(1, 1, values.length, values[0].length).setValues(values); main.getRange(1, 1, 1, col).copyFormatToRange(sheet, 1, col, 1, 1); main.getRange("A2:2").copyFormatToRange(sheet, 1, col, 2, values.length); } }); }En este script de ejemplo, los valores se recuperan de "Main_Sheet". Y recupera cada valor para cada hoja, y cada valor se coloca en cada hoja.
Acerca de su solicitud adicional de Can you please include headers as well. Right now the headers are missing. Once you add headers I will test again , lo reflejé.
Acerca de su solicitud adicional de the header row is in bold values in the "Main_Sheet" tab. With your code, the headers are just being copied as plain text. I want the format of header to be same in all tabs , lo reflejé.
Acerca de su solicitud adicional de I am saying that I want to keep the format from the main_sheet tab consistent with other tabs. So if Main_sheet has blue and bold headers, the code should copy the header value and format into the new tab. , lo reflejé.
Acerca de su solicitud adicional de The goal is the paste the values from Main_Sheet to the correct tabs. The format(of header and all other rows) in main_sheet should be same as Central, East, West Tab. Right now your code is pasting plain values(it is not pasting the correct format from the Source sheet--> Main_Sheet)... Right now only header row has the correct format in the output tabs. Please preserve the format of the non header rows as well , lo reflejé.