Skip to content

Instantly share code, notes, and snippets.

@theGove
Last active April 24, 2022 05:42
Show Gist options
  • Save theGove/d24a5ed93c4108484d9819fbcdadffa3 to your computer and use it in GitHub Desktop.
Save theGove/d24a5ed93c4108484d9819fbcdadffa3 to your computer and use it in GitHub Desktop.
A Tool to Generate a Unique Set of Random Numbers
async function auto_exec(){
Jade.set_css(gist_files('style.css'))
console.log('made it here',gist_files('random_number.html'))
tag("tools-body").innerHTML=gist_files('random_number.html')
const tool_data = await jade.read_object_from_workbook(window.active_tool)
console.log("tool_data",tool_data)
for(const[key,value] of Object.entries(tool_data)){
console.log(key, value)
tag(key).value=value
}
}
async function save_props(){
const the_object={result:"it works with the active tool"}
await jade.save_object_to_workbook(the_object, window.active_tool)
}
async function read_props(){
const obj = await jade.read_object_from_workbook(window.active_tool)
console.log("obj",obj)
}
function default_value(input){
if(!input.value){
console.log("getting address of active cell")
Excel.run(async (excel) => {
let rng = excel.workbook.getSelectedRange();
rng.load("address");
await excel.sync();
input.value = rng.address.split("!")[1]
})
}
}
async function generate_random_numbers(){
const lower_bound = parseInt(tag("lower-bound").value)
const upper_bound = parseInt(tag("upper-bound").value)
await jade.save_object_to_workbook({
"lower-bound":lower_bound,
"upper-bound":upper_bound
}, window.active_tool)
//set: the address of the target cell, value: the target value for the cell, change: the address of a precedent cell that will change to achieve the goal
Excel.run(async function(excel){
const set_size = upper_bound-lower_bound+1
let random_number_set = []
let iterations = 0
do{
const temp_number = random_number(lower_bound,upper_bound)
if(random_number_set.includes(temp_number)===false) {random_number_set.push(temp_number)}
iterations++
}while(random_number_set.length<set_size)
console.log('random_number_set',random_number_set)
console.log('iterations',iterations)
let data_array = []
for(const rand_num of random_number_set){
data_array.push([rand_num])
}
console.log('data_array',data_array)
const first_cell = excel.workbook.getActiveCell()
const complete_range = first_cell.getAbsoluteResizedRange(set_size,1)
complete_range.values = data_array
await excel.sync()
/*
let html = ['<table><th>Numbers</th>']
for(const rand_num of random_number_set){
html.push(`<tr><td>${rand_num}</td></tr>`)
}
tag("results").innerHTML = html.join('')
*/
function random_number(min,max){
const random_num = Math.floor(Math.random() * (max-min+1))+min
return random_num
}
})
}
<div style="margin:1rem; text-align: center;"><div style="display:inline-block; text-align: left;">
<table>
<tbody>
<tr>
<td>Lower Bound:</td>
<td><input size="6" type="text" id="lower-bound" /></td>
</tr>
<tr>
<td>Upper Bound:</td>
<td><input size="6" type="text" id="upper-bound" /></td>
</tr>
<tr>
<td colspan="2" align="right"><button onclick="jade_modules.code.generate_random_numbers()">Generate Numbers</button></td>
</tr>
</tbody>
</table>
</div></div>
<div style="margin:1rem; text-align: center;"><div style="display:inline-block; text-align: left;">
<table>
<tbody>
<tr>
<td>Lower Bound:</td>
<td><input size="6" type="text" id="lower-bound" /></td>
</tr>
<tr>
<td>Upper Bound:</td>
<td><input size="6" type="text" id="upper-bound" /></td>
</tr>
<tr>
<td colspan="2" align="right"><button onclick="jade_modules.code.generate_random_numbers()">Generate Numbers</button></td>
</tr>
</tbody>
</table>
</div>
<div id="results">
</div>
</div>
.light{
color:red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment