# coding: utf-8 from __future__ import unicode_literals import sys, os import requests import importlib # to run separatly from soffice # $ soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" # uncomemnt for run separatly from soffice # sys.path.append("/lib64/libreoffice/program/") from scriptforge import ScriptForge, CreateScriptService from unohelper import fileUrlToSystemPath ui = CreateScriptService("UI") doc = CreateScriptService("Calc") bas = CreateScriptService("Basic") url = fileUrlToSystemPath(XSCRIPTCONTEXT.getDocument().URL ) curpath = '/'.join([url.split('/')[x] for x in range(0,len(url.split('/'))-1)]) if not ui.Activate(url) : bas.MsgBox("Error no window active",bas.MB_OK) def _load_module(): #[start] comment for run embeded module if (not 'gquiz' in sys.modules) or (not 'moodleQuiz' in sys.modules): doc = XSCRIPTCONTEXT.getDocument() urlem = '{}/{}'.format(url,'/Scripts/python/Library') sys.path.insert(0, urlem) _statusBarInfoUpdate("Load embedded modul link form {}".format(url),0) #[end] #[start] uncomemnt for run separatly from soffice #sys.path.insert(0, '{}/{}'.format(os.getcwd(),'Library')) #ScriptForge(hostname='localhost', port=2002) #[end] def MakeTemplate(): doc.SetArray(doc.CurrentSelection, \ (("","", \ "", "", "", \ "", "", "", \ "", "", "", \ "", "", "", \ "", "", "", ),)) def _statusBarInfoUpdate(text : str, progress : int): ui.ShowProgressBar("loading...", "{}% {}".format(progress,text), progress) def _updateQuestion(q): selctedCell = doc.CurrentSelection cwd = curpath maxRow = doc.LastRow(selctedCell)+1-doc.FirstRow(selctedCell) for nrow in range(0, maxRow): q.setProgress(int(((nrow+1)/maxRow)*100)) item = doc.getValue(doc.Offset(doc.FirstCell(selctedCell),nrow,0,1,17)) opt = [] theAnswer = -1 c = 1 for o in range(2,17,3): if (item[o] == 1): theAnswer = c if (item[o+1] != ""): opt.append(q.createOption("{}. {}".format(chr(64+c),item[o+2]),cwd+item[o+1])) else: opt.append(q.createOption("{}. {}".format(chr(64+c),item[o+2]))) c = c+1 if (theAnswer == -1): raise Exception("Chose the correct answer") if( item[1] != ""): img = cwd+item[1] else: img = None qq = q.createQuestion(title = "Soal No {}".format(nrow+1),\ description = item[0],\ indexAnswer = theAnswer, \ options = opt, itemImage=img) q.submitQuestion(nrow,qq) q.update() def GoogleQuiz(): _load_module() from gquiz import gquiz q = gquiz() q.setSavePath(curpath) q.AttachProcessInfo(_statusBarInfoUpdate) q.setProgress(0) q.generateService() q.createForm("Demo Soal") _updateQuestion(q) ui.SetStatusbar("creating google form quiz done!") _statusBarInfoUpdate("Finish!!",100) bas.InputBox("Open link to edit your form:","Your Google Form Quiz, done!", "{}".format(q.resultUri)) def MoodleQuiz(): _load_module() from moodleQuiz import moodleQuiz q = moodleQuiz() q.setSavePath(curpath) q.AttachProcessInfo(_statusBarInfoUpdate) q.setProgress(0) _updateQuestion(q) ui.SetStatusbar("Done!") _statusBarInfoUpdate("Check *.xml file in curent folder!",100) g_exportedScripts = (MakeTemplate, GoogleQuiz, MoodleQuiz)