Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
¿Hay alguna manera de agregar valores a algunas celdas aleatorias de un rango?

Tengo el siguiente código:

 function test1() { var app = SpreadsheetApp; var activesheet = app.getActiveSpreadsheet().getActiveSheet(); for (var j = 2; j<= 3; j++) { activesheet.getRange(j,2).setValue("C"); } }

Este código agregará el carácter "C" a la celda B2 y B3, lo cual es genial.

Estoy buscando obtener mi código para agregar dos caracteres "C" a 2 celdas aleatorias de las primeras 30 celdas de la columna B.

Cualquier ayuda es muy apreciada. Gracias.

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Agrega una cadena de caracteres al rango n veces haciendo una selección única cada vez.

 function addCharsRandom(chr = "AB", a1rg = "", n = 3) { const ss = SpreadsheetApp.getActive(); const sh = ss.getActiveSheet(); const rg = (a1rg.length < 2) ? sh.getActiveRange() : sh.getRange(a1rg); if (rg.getDisplayValues().flat().length >= n) { rg.setBackground("yellow") const w = rg.getWidth(); const h = rg.getHeight(); const r = rg.getRow(); const c = rg.getColumn(); let rA = []; let cA = []; [...Array.from(new Array(n).keys())].forEach(i => { let row, col; do { row = r + Math.floor(Math.random() * h); col = c + Math.floor(Math.random() * w); } while (rA.indexOf(row)>-1 && cA.indexOf(col)>-1); rA.push(row); cA.push(col); sh.getRange(row, col).setValue(sh.getRange(row, col).getDisplayValue() + chr).setBackground("white"); }); } else { SpreadsheetApp.getUi().alert("Range is to small for number of unique selections"); } }

si a1rg es igual a "", entonces seleccionará el rango activo.

Manifestación:

ingrese la descripción de la imagen aquí

about 4 years ago · Santiago Gelvez Report

0

Este código puede ayudarte

 function test1() { var app = SpreadsheetApp; var activesheet = app.getActiveSpreadsheet().getActiveSheet(); let randomCellIndex; let usedCell = []; for (var i = 0; i < 2; i++) { // make sure not to use same index twice do randomCellIndex = Math.floor(Math.random() * 30) + 1; while (usedCell.indexOf(randomCellIndex) !== -1); usedCell.push(randomCellIndex); activesheet.getRange(randomCellIndex,2).setValue("CC"); } }
about 4 years ago · Santiago Gelvez Report

0

Supongo que no desea valores duplicados, por lo que tomaría dos valores de la matriz barajada. Para evitar entrar en la madriguera del conejo con el ejemplo de matriz aleatoria, se usa el guión bajo

 _.shuffle([...Array(30)].map((value, index) => index+1))
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!