Issue with setValues - google-apps-script

I have this code :
function calculateTotals(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var total, clientID,cellValues,cellFontLines, result;
for (i=0;i<98;i++) {
//sheet.getRange("K"+i).setValue("calculating");
clientID=sheet.getRange("J3:J100").getValues();
total=0;
cellValues=sheet.getRange("B3:H234").getValues();
cellFontLines=sheet.getRange("B3:H234").getFontLines();
result=sheet.getRange("K3:K100").getValues(); // this is dumb
if (clientID[i][0] != "") {
for (j=0;j<=6;j++) {
for (k=0;k<=231;k++) {
if (cellValues[k][j] == clientID[i][0] && cellFontLines[k][j] != 'line-through' ) {
total++;
}
}
}
result[i][0]=total/2;
Logger.log(i,clientID[i][0],result[i][0]);
//sheet.getRange("K"+(i+3)).setValue(result[i][0]);
} else {
break;
}
}
sheet.getRange("K3:K100").setValues(result); // this doesn't seem to do anything
Logger.log(result);
}
I can't figure out how to use setValues. I logged it all, and it looks right, in the debugger the array is all set but I still have to use the commented out setValue() line to update that column.
As an aside, is there a better way to initialize result to the correct dimensions than just reading in the previous results?

This works :
function calculateTotals(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var total, clientID,cellValues,cellFontLines, result=new Array(98);
clientID=sheet.getRange("J3:J100").getValues();
cellValues=sheet.getRange("B3:H234").getValues();
cellFontLines=sheet.getRange("B3:H234").getFontLines();
result=sheet.getRange("K3:K100").getValues();
for (i=0;i<98;i++) {
//sheet.getRange("K"+i).setValue("calculating");
total=0
if (clientID[i][0] != "") {
for (j=0;j<=6;j++) {
for (k=0;k<=231;k++) {
if (cellValues[k][j] == clientID[i][0] && cellFontLines[k][j] != 'line-through' ) {
total++;
}
}
}
result[i][0]=total/2;
} else {
break;
}
}
sheet.getRange("K3:K100").setValues(result);
}

Related

I want to make everyone able to upload files

**Hello in my site,**
no one can upload files except those who own the names of these ranks, how can I make everyone who has a rank be able to upload files
Blockquote
I want to make everyone be able to upload files
Blockquote
app.post("/upload", function (taylinn, tetsuo) {
var sylvian = orpah;
UsersRepo.getByToken(taylinn.query.token).then(function (rubith) {
var kyele = sylvian;
if (rubith) {
if (taylinn.query.secid == "u") {
islink = "/sendfile";
upload(taylinn, tetsuo, function (yalanda) {
if (yalanda) {
tetsuo.send(yalanda);
} else {
tetsuo.end("/sendfile/" + taylinn.file.filename);
}
});
} else {
if (rubith.power == "admin" || rubith.power == "chatmaster" || rubith.power == "rank" || rubith.power == "power" || rubith.power == "Hide") {
islink = "/" + taylinn.query.state;
upload(taylinn, tetsuo, function (aya) {
if (aya) {
tetsuo.send(aya);
} else {
tetsuo.end(JSON.stringify({err: ![], msg: taylinn.file.filename}));
}
});

Auto Trim Clean Proper (Title) Case and remove extra spaces between words in Google spreadsheet

In the continuation of the question asked HERE and a great reply by #Edvin I have following query.
The following code works great in my Google spreadsheet.
function onEdit(e) {
if (typeof e.value != 'object') {
if ([4, 5].indexOf(e.range.columnStart)<0) return;
e.range.setValue(titleCase(e.value));
}
}
function titleCase(str) {
return str.toString().split(/\b/).map(function(word) {
return word ? word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() : '';
}).join('');
}
Is there any way I can add TRIM, CLEAN, Remove extra spaces between words like functions in this code.
How about this answer? Please think of this as just one of several answers.
In this answer, I used the script from this thread for achieving TRIM and CLEAN. And using this function, your script was modified.
Modified script:
function onEdit(e) {
if (typeof e.value != 'object') {
if ([4, 5].indexOf(e.range.columnStart)<0) return;
e.range.setValue(titleCase(e.value));
}
}
// Modified
function titleCase(str) {
return str.toString().split(/\b/).map(function(word) {
return word ? cleanForGAS(word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) : '';
}).filter(String).join(' ');
}
// This function is from https://stackoverflow.com/a/50053581
function cleanForGAS(str) {
if (typeof str == "string") {
var escaped = escape(str.trim());
for (var i = 0; i <= 31; i++) {
var s = i.toString(16);
var re = new RegExp("%" + (s.length == 1 ? "0" + s : s).toUpperCase(), "g");
escaped = escaped.replace(re, "");
}
var remove = ["%7F", "%81", "%8D", "%8F", "%90", "%9D"];
remove.forEach(function(e) {
var re = new RegExp(e, "g");
escaped = escaped.replace(re, "");
});
return unescape(escaped).trim();
} else {
return str;
}
}
If I misunderstood your question and this was not the result you want, I apologize.

Actionscript display text on screen once an action is performed

So I need to display a text once an action is performed but even though I tried to do so by using dynamic text and labels, I didn't manage to finish my programming due to errors:
var group:RadioButtonGroup= new RadioButtonGroup ("Question1");
var group2:RadioButtonGroup= new RadioButtonGroup ("Question2");
var group3:RadioButtonGroup= new RadioButtonGroup ("Question3");
var group4:RadioButtonGroup= new RadioButtonGroup ("Question4");
var group5:RadioButtonGroup= new RadioButtonGroup ("Question5");
var counterT:int;
var counterF:int;
submit.buttonMode=true;
counterT=0;
counterF=0;
t1.group = group;
f1.group = group;
t2.group=group2;
f2.group=group2;
t3.group=group3;
f3.group=group3;
t4.group=group4;
f4.group=group4;
t5.group=group5;
f5.group=group5;
submit.label="Submit";
submit.addEventListener(MouseEvent.CLICK,submitanswer);
function submitanswer (event:MouseEvent): void {
if (group.selection == t1) {
counterT==counterT+1
}
else
if (group.selection==f1) {
counterF==counterF+1;
}
}
if (group2.selection ==t2) {
counterT==counterT+1
}
else
if (group2.selection==f2) {
counterF==counterF+1
}
if (group3.selection ==t3) {
counterT==counterT+1
}
else
if (group3.selection==f3) {
counterF==counterF+1
}
if (group4.selection ==t4) {
counterT==counterT+1
}
else
if (group4.selection==f4) {
counterF==counterF+1
}
if (group5.selection ==t5) {
counterT==counterT+1
}
else
if (group5.selection==f5) {
counterF==counterF+1
}
The first thing I see is that you are using the == to set a value. You need to use = when setting values. So like this:
if (x == y) {
counter = counter + 1;
}
or you can just use counter++ like this
if (x == y) {
counter++;
}

Html web storage

I have to store value using HTML local storage. But i am not storing values directly. Values are coming from from xml which i am parsing. Please help me out how should i store the values.
The snippet code is in javascript is:
function html5_storage_support() {
try {
return 'localStorage' in window && window['localStorage'] == null;
} catch (e) {
return false;
}
}
unction checekAnswer() {
$("#btnSubmit").attr("disabled","true");
$("#btnSubmit").attr("onclick","");
attempted_count++;
var feedback=question[i].getElementsByTagName("feedback");
var answer=question[i].getElementsByTagName("answer");
var score=question[i].getElementsByTagName("score");
var left=question[i].getElementsByTagName("answer")[0].getAttribute("left");
var top=question[i].getElementsByTagName("answer")[0].getAttribute("top");
var single=question[i].getElementsByTagName("image")[0].getAttribute("single");
if(answer[0].getAttribute("mcq")=="true")
{
var ans=$('input:radio[name=rdOptions]:checked').val();
var correct;
var option=answer[0].getElementsByTagName("options");
for(var k=0;k<option.length;k++)
{
if(option[k].getAttribute("correctAnswer")=="true")
{
correct=option[k].getAttribute("value");
}
}
if(ans==correct)
{
correctAnswers++;
$("#feedback").html("Correct Answer:"+ans+"<br/>");
$("#feedback").append(score[0].childNodes[0].nodeValue);
$("#feedback").append("<br/>");
$("#feedback").append(feedback[0].childNodes[0].nodeValue);
}
else
{
$("#feedback").html("Your Answer:"+ans+"<br/>");
$("#feedback").append("Correct Answer:"+correct+"<br/>");
$("#feedback").append(score[1].childNodes[0].nodeValue);
$("#feedback").append("<br/>");
$("#feedback").append(feedback[0].childNodes[0].nodeValue);
}
}
else
{
$("#draggableImg").draggable({disabled:true});
var diff=0;
if(single=="true")
diff=2;
else
diff=7;
if(xPos<=(parseInt(left)+7) && xPos>=(parseInt(left)-7) && yPos<=(parseInt(top)+diff) && yPos>=(parseInt(top)-diff))
{
**//Displaying the feedbacks and scores**
correctAnswers++;
$("#feedback").append(score[0].childNodes[0].nodeValue);
$("#feedback").append("<br/>");
$("#feedback").append(feedback[0].childNodes[0].nodeValue);
}
else
{
$("#feedback").html(score[1].childNodes[0].nodeValue);
$("#feedback").append("<br/>");
$("#feedback").append(feedback[0].childNodes[0].nodeValue);
}
//$("#counter").html("left="+xPos+",top="+yPos);
$("#trFeedBack").show("slow");
display_nav(j,obj)
}
} // end function

firebug saying not a function

<script type = "text/javascript">
var First_Array = new Array();
function reset_Form2() {document.extraInfo.reset();}
function showList1() {document.getElementById("favSports").style.visibility="visible";}
function showList2() {document.getElementById("favSubjects").style.visibility="visible";}
function hideProceed() {document.getElementById('proceed').style.visibility='hidden';}
function proceedToSecond ()
{
document.getElementById("div1").style.visibility="hidden";
document.getElementById("div2").style.visibility="visible";
document.getElementById("favSports").style.visibility="hidden";
document.getElementById("favSubjects").style.visibility="hidden";
}
function backToFirst () {
document.getElementById("div1").style.visibility="visible";
document.getElementById("div2").style.visibility="hidden";
document.getElementById("favSports").style.visibility="visible";
document.getElementById("favSubjects").style.visibility="visible";
}
function reset_Form(){
document.personalInfo.reset();
document.getElementById("favSports").style.visibility="hidden";
document.getElementById("favSubjects").style.visibility="hidden";
}
function isValidName(firstStr) {
var firstPat = /^([a-zA-Z]+)$/;
var matchArray = firstStr.match(firstPat);
if (matchArray == null) {
alert("That's a weird name, try again");
return false;
}
return true;
}
function isValidZip(zipStr) {
var zipPat =/[0-9]{5}/;
var matchArray = zipStr.match(zipPat);
if(matchArray == null) {
alert("Zip is not in valid format");
return false;
}
return true;
}
function isValidApt(aptStr) {
var aptPat = /[\d]/;
var matchArray = aptStr.match(aptPat);
if(matchArray == null) {
if (aptStr=="") {
return true;
}
alert("Apt is not proper format");
return false;
}
return true;
}
function isValidDate(dateStr) {
//requires 4 digit year:
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null) {
alert("Date is not in a valid format.");
return false;
}
return true;
}
function checkRadioFirst() {
var rb = document.personalInfo.salutation;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please specify a salutation");
return false;
}
function checkCheckFirst() {
var rb = document.personalInfo.operatingSystems;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please specify an operating system") ;
return false;
}
function checkSelectFirst() {
if ( document.personalInfo.sports.selectedIndex == -1)
{
alert ( "Please select a sport" );
return false;
}
return true;
}
function checkRadioSecond() {
var rb = document.extraInfo.referral;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please select form of referral");
return false;
}
function checkCheckSecond() {
var rb = document.extraInfo.officeSupplies;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please select an office supply option");
return false;
}
function checkSelectSecond() {
if ( document.extraInfo.colorPick.selectedIndex == 0 ) {
alert ( "Please select a favorite color" );
return false;
}
return true;
}
function check_Form(){
var retvalue = isValidDate(document.personalInfo.date.value);
if(retvalue) {
retvalue = isValidZip(document.personalInfo.zipCode.value);
if(retvalue) {
retvalue = isValidName(document.personalInfo.nameFirst.value);
if(retvalue) {
retvalue = checkRadioFirst();
if(retvalue) {
retvalue = checkCheckFirst();
if(retvalue) {
retvalue = checkSelectFirst();
if(retvalue) {
retvalue = isValidApt(document.personalInfo.aptNum.value);
if(retvalue){
document.getElementById('proceed').style.visibility='visible';
var rb = document.personalInfo.salutation;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
var salForm = rb[i].value;
}
}
var SportsOptions = "";
for(var j=0;j<document.personalInfo.sports.length;j++){
if ( document.personalInfo.sports.options[j].selected){
SportsOptions += document.personalInfo.sports.options[j].value + " ";
}
}
var SubjectsOptions= "";
for(var k=0;k<document.personalInfo.subjects.length;k++){
if ( document.personalInfo.subjects.options[k].selected){
SubjectsOptions += document.personalInfo.subjects.options[k].value + " ";
}
}
var osBox = document.personalInfo.operatingSystems;
var OSOptions = "";
for(var y=0;y<osBox.length;y++) {
if(osBox[y].checked) {
OSOptions += osBox[y].value + " ";
}
}
First_Array[0] = salForm;
First_Array[1] = document.personalInfo.nameFirst.value;
First_Array[2] = document.personalInfo.nameMiddle.value;
First_Array[3] = document.personalInfo.nameLast.value;
First_Array[4] = document.personalInfo.address.value;
First_Array[5] = document.personalInfo.aptNum.value;
First_Array[6] = document.personalInfo.city.value;
for(var l=0; l<document.personalInfo.state.length; l++) {
if (document.personalInfo.state.options[l].selected) {
First_Array[7] = document.personalInfo.state[l].value;
}
}
First_Array[8] = document.personalInfo.zipCode.value;
First_Array[9] = document.personalInfo.date.value;
First_Array[10] = document.personalInfo.phone.value;
First_Array[11] = SportsOptions;
First_Array[12] = SubjectsOptions;
First_Array[13] = OSOptions;
alert("Everything looks good.");
document.getElementById('validityButton').style.visibility='hidden';
}
}
}
}
}
}
}
}
/*function formAction2() {
var retvalue;
retvalue = checkRadioSecond();
if(!retvalue) {
return retvalue;
}
retvalue = checkCheckSecond();
if(!retvalue) {
return retvalue;
}
return checkSelectSecond() ;
} */
</script>
This is just a sample of the code, there are alot more functions, but I thought the error might be related to surrounding code. I have absolutely no idea why, as I know all the surrounding functions execute, and First_Array is populated.
However when I click the Proceed to Second button, the onclick attribute does not execute because Firebug says proceedToSecond is not a function
button code:
<input type="button" id="proceed" name="proceedToSecond" onclick="proceedToSecond();" value="Proceed to second form">
I had the same problem, and it's because you have a form with the same name as your function. JavaScript doesn't seem to be able to distinguish between the two, so you get the "not a function" error.
Maybe there is an error in your Javascript before that snippet given by you. If so, the rest will not be parsed by Firefox and then your function will not be defined.
The problem was resolved by changing proceedToSecond() to doIt() in both the call and the function name. I have no idea why
Hmm... I always used javascript:function_name() instead of function_name() because a few times the javascript didn't run. The name tag for the html snippet might have to be changed to a slightly different name because javascript might be getting it mixed up. Can you show us the entire javascript file because there may be a mistake/syntax error somewhere at the bottom/top.
It works on my computer, Firefox 3.5.5, Firebug 1.4.3, when inserting your code into an empty html document (<html><head/><body> code </body></html>)
Maybe there is another bug somewhere in your DOM, or in some of your other functions?
Could you possibly paste the entire source here, or maybe on a pastebin site?