Skip to content

Instantly share code, notes, and snippets.

@theGove
Last active April 23, 2022 13:58
Show Gist options
  • Save theGove/fb0c621005d160026cb9c48a6a162247 to your computer and use it in GitHub Desktop.
Save theGove/fb0c621005d160026cb9c48a6a162247 to your computer and use it in GitHub Desktop.
Excel Assignment Creation Tools
//e7c158b8544aaacf2e2dce2a6acfcd52
async function auto_exec(){
Jade.set_css(gist_files('style.css'))
tag("tools-body").innerHTML=gist_files('tool.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 get_address(tag_name){
tag('datarange').value=await selected_range_address()
}
async function selected_range_address(){
let address=""
await Excel.run(async (excel) => {
let rng = excel.workbook.getSelectedRange();
rng.load("address");
await excel.sync();
address=rng.address.split("!")[1]
})
return address
}
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 split_data(){
// save the defaults for the tool
await jade.save_object_to_workbook(
get_form_values(["datarange","trainingprecent"])
, window.active_tool)
Excel.run(async function(excel){
const sheet = excel.workbook.worksheets.getActiveWorksheet()
const datarange = sheet.getRange(tag("datarange").value)
datarange.load('columnCount')
await excel.sync()
datarange.getColumnsAfter(1).insert(Excel.InsertShiftDirection.right);
const next_col = datarange.getColumnsAfter(1)
next_col.load('values')
await excel.sync()
const train=[["Train"]]
for(let x=1;x<next_col.values.length;x++){
if(Math.random()*100>parseFloat(tag("trainingprecent").value)){
train.push([false])
}else{
train.push([true])
}
}
next_col.values=train
await excel.sync()
})
}
function get_form_values(tag_names){
const obj={}
for(const tag_name of tag_names){
obj[tag_name]=tag(tag_name).value
}
return obj
}
<div style="margin:1rem; text-align: center;"><div style="display:inline-block; text-align: left;">
<table>
<tbody>
<tr>
<td colspan="2" style="text-align: center;background-color: #aaa;font-weight: bold;">Split Data Set</td>
</tr>
<tr>
<td>Data Range:</td>
<td><input size="6" type="text" id="datarange" onfocus="jade_modules.code.default_value(this)"/>
<i onclick="jade_modules.code.get_address('datarange')" class="fa-solid fa-table" style="color:#777"></i></td>
</tr>
<tr>
<td>Training Set %:</td>
<td><input size="6" type="text" id="trainingprecent" /></td>
</tr>
<tr>
<td colspan="2" align="right"><button onclick="jade_modules.code.split_data()">Split Data Set</button></td>
</tr>
</tbody>
</table>
</div></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment