Código para un bucle tipo for in
function myFunction() {
**//defininos el archivo y el rango con el que vamos a trabajar//**
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheets();
**//Definimos la variable donde van a estar los datos donde vamos a iterar//**
const data = sheet[0].getDataRange().getValues();
for (i in data) {
const row = data[i];
const tipoenvio = row[8];
const inciobucle = 1 + +i;
if (tipoenvio === "Express (Menor a una hora)"){
sheet[0].getRange(inciobucle, 10).setValue("14500");
}else if(tipoenvio === "AM/PM (Some day)"){
sheet[0].getRange(inciobucle, 10).setValue("8000");
}else if(tipoenvio === "Next Day"){
sheet[0].getRange(inciobucle, 10).setValue("6500");
}else if(tipoenvio === "2-3 Días"){
sheet[0].getRange(inciobucle, 10).setValue("6000");
}else if(tipoenvio === "5-7 Días Hábiles"){
sheet[0].getRange(inciobucle, 10).setValue("9000");
}else{
sheet[0].getRange(inciobucle, 10).setValue("0");
}
}
**//esta variable nos pone el título de la columna cuando el inicio del bucle arranca
en la fila 1//**
sheet[0].getRange("J1").setValue("Costo Envío");
}
El código anterior crea un bucle tipo for in, el cual nos permite tener un bucle sin preocuparnos por el indice, en este caso estamos escribiendo en la columna de costo envío el valor de un flete según el valor de la columna tipo de envío.
Comentarios
Publicar un comentario