Soy muy nuevo en el script de la aplicación de Google y he estado jugando tratando de hacer que un código funcione. Básicamente, estaba tratando de copiar una celda y pegarla en la siguiente celda en blanco en un rango específico de celdas.
¡Yo tengo que trabajar! Pero me pregunto si hay una forma más optimizada/mejor de escribir esto como un momento de aprendizaje.
function pastenext() { var ss= SpreadsheetApp.getActiveSpreadsheet(); var s=ss.getSheetByName('Questions') var r=s.getRange("C8:C17"); var up = s.getRange("E5") if (r.getCell(1, 1).isBlank()) { r.getCell(1, 1).setValue(up.getValue()) } else if (r.getCell(2, 1).isBlank()) { r.getCell(2, 1).setValue(up.getValue()) } else if (r.getCell(3, 1).isBlank()) { r.getCell(3, 1).setValue(up.getValue()) } else if (r.getCell(4, 1).isBlank()) { r.getCell(4, 1).setValue(up.getValue()) } else if (r.getCell(5, 1).isBlank()) { r.getCell(5, 1).setValue(up.getValue()) } else if (r.getCell(6, 1).isBlank()) { r.getCell(6, 1).setValue(up.getValue()) } else if (r.getCell(7, 1).isBlank()) { r.getCell(7, 1).setValue(up.getValue()) } else if (r.getCell(8, 1).isBlank()) { r.getCell(8, 1).setValue(up.getValue()) } else if (r.getCell(9, 1).isBlank()) { r.getCell(9, 1).setValue(up.getValue()) } else if (r.getCell(10, 1).isBlank()) { r.getCell(10, 1).setValue(up.getValue()) } }Solo usa loop para hacerlo mucho más corto, a partir de esto:
if (r.getCell(1, 1).isBlank()) { r.getCell(1, 1).setValue(up.getValue()) } else if (r.getCell(2, 1).isBlank()) { r.getCell(2, 1).setValue(up.getValue()) } else if (r.getCell(3, 1).isBlank()) { r.getCell(3, 1).setValue(up.getValue()) } else if (r.getCell(4, 1).isBlank()) { r.getCell(4, 1).setValue(up.getValue()) } else if (r.getCell(5, 1).isBlank()) { r.getCell(5, 1).setValue(up.getValue()) } else if (r.getCell(6, 1).isBlank()) { r.getCell(6, 1).setValue(up.getValue()) } else if (r.getCell(7, 1).isBlank()) { r.getCell(7, 1).setValue(up.getValue()) } else if (r.getCell(8, 1).isBlank()) { r.getCell(8, 1).setValue(up.getValue()) } else if (r.getCell(9, 1).isBlank()) { r.getCell(9, 1).setValue(up.getValue()) } else if (r.getCell(10, 1).isBlank()) { r.getCell(10, 1).setValue(up.getValue()) }A esto:
for(var i = 1; i <= 10; i++) if (r.getCell(i, 1).isBlank()) { // If the specific cell found is blank r.getCell(i, 1).setValue(up.getValue()); break; // exit the loop after set value } El uso de break es igual que el else if en el largo anterior, por lo que solo encontrará la condición satisfactoria para los datos actuales y luego ignorará las otras condiciones o el otro condicional else if los bloques se omiten/ignoran después de encontrar la condición satisfactoria. condición.