Can I create array based on field names that exist in PDF? - arrays

I am trying to count the number of a specific field which is not empty and would like to use an array to store the field names based on whether they exist in the PDF form.
My code looks like this so far and it works as long as the field names in the array exists, if that field does not exist then the code breaks.
The SiteDeficiency_DeficiencyNumber_# could have 0 instances or anywhere up to 50 instances or so.
I did find that you can check if a name exists with this code but I can't seem to get it to work.
Any help is appreciated. I have coded simple things but maybe this is just too advanced for me.
This is what my code looks like so far:
var aFields = new Array("SiteDeficiency_DeficiencyNumber_1",
"SiteDeficiency_DeficiencyNumber_2",
"SiteDeficiency_DeficiencyNumber_3",
"SiteDeficiency_DeficiencyNumber_4",
"SiteDeficiency_DeficiencyNumber_5",
"SiteDeficiency_DeficiencyNumber_6");
var count = 0;
for (i = 0; i < aFields.length; i++) {
if (this.getField(aFields[i]).valueAsString != "") count++
}
event.value = count;
I tried incorporating this too
if(document.forms[0].SiteDeficiency_DeficiencyNumber_1)
{
...
}
I would really like the array at the beginning to populate with only the SiteDeficiency_DeficiencyNumber_# fields that exist and then have the array looped through to count which fields are not blank.
I tried doing this but still does not work.
var aFields = [];
for(i = 1; i <101; i++)
{
if(document.forms[0].("SiteDeficiency_DeficiencyNumber_"+i).valueAsString.length > 0) //Checks if field exists
{
aFields.push("SiteDeficiency_DeficiencyNumber_"+i); //adds field name to array
}
}
var count = 0;
for (a = 0; a < aFields.length; a++) {
if (this.getField(aFields[a]).valueAsString != "") count++ //counts field if not empty
}
event.value = count;

Try this code:
var count = 0;
for(var i = 0; i < this.numFields; i++)
{
if (this.getNthFieldName(i).indexOf("SiteDeficiency_DeficiencyNumber")!= -1){
if (this.getField(this.getNthFieldName(i)).valueAsString != "")
count = count +1
}
}
event.value = count;
The first if statement will check if name of the field contain string SiteDeficiency_DeficiencyNumber. If SiteDeficiency_DeficiencyNumber is present in the field naem then it will check the field value and increment accordingly.
Don't forget to upvote! let me know if you need anything else

Related

Value not returning right

I am having a issue getting my code to return the correct value.
I am building spreadsheet to help me with cooking for a video game I play. I am trying to track the amount of all the ingredients I have for cooking. I want to be able to update the amount I have from a dashboard rather than go scroll through the list to find each ingredient individually.
Here is what I have setup so far.
I have a page called Updater where I am selecting from a drop down a certain ingredient(cell A2). Next to that drop down is cell for me to enter a new amount of that ingredient that I have(cell B2).
The second page is called Ingredients. This has a 2 columns. Column 1 is a list of the ingredients and in column 2 is the number I currently have on hand.
The script I have written so far can find me the row number, but when I try to get the current amount I have for the ingredient to make sure the code is working, it doesnt return anything in the log.
There is my code
function finalTest(){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var fs = spreadsheet.getSheetByName("Ingredients");
var ts = spreadsheet.getSheetByName("Updater");
var data = fs.getDataRange().getValues();
var luItem = ts.getRange("A2").getValue();
var newAmt = ts.getRange("B2").getValue();
var rn = 0;
Logger.log(rn);
Logger.log(newAmt);
Logger.log(data);
Logger.log(luItem);
for(var i = 0; i<data.length;i++){
if(data[i][0] == luItem){ //[1] because column B
Logger.log((i+1))
return i+1;
}
}
rn = i;
Logger.log(rn);
var fsr = fs.getRange(rn,2).getValue();
Logger.log(fsr);
}
Right now the logger will return up to where it logs "i", but will not return the new rn or fsr values.
Any help would be greatly appreciated.
A function immediately returns on the return statement. So you can only return one value there - or an object with multiple values inside.
As it looks that you just want to log data out, you would have to do that inside the if, but before the return to reach:
for (var i = 0; i < data.length; i++) {
if (data[i][0] == luItem) {
// first do all the logging
Logger.log(i + 1);
rn = i;
Logger.log(rn);
var fsr = fs.getRange(rn,2).getValue();
Logger.log(fsr);
// and then return from the function
return i + 1;
}
// what should be returned here??
}
Please also note that you should return a value or handle it somehow else when the loop runs through and the condition is never met.
If the return value then is still i + 1, you could make the if-condition instead part of the stop condition of the loop:
var i = 0;
for (; (i < data.length) && (data[i][0] == luItem); i++);
// no loop body needed here!
// do the logging outside the loop
Logger.log(i + 1);
rn = i;
Logger.log(rn);
var fsr = fs.getRange(rn,2).getValue();
Logger.log(fsr);
// and then return from the function
return i + 1;

Search for a value in a sheet using loop in Google Apps Script

The below code searches for a value found in SheetOne, Cell B60, in SheetTwo, Column A.
If the value is found in column A of SheetTwo, a message box will display the value of the cell adjacent to where the cell was found. My problem is though the code works, I would also like a message box to appear if no match is found. Everything I have tried either traps me in the loop, or yields no result.
{
var MyWB = SpreadsheetApp.getActiveSpreadsheet();
var ValueSheet = MyWB.getSheetByName("SheetOne");
var SearchValue = ValueSheet.getRange("B60").getValue()
var SearchSheet = MyWB.getSheetByName("SheetTwo");
var SearchRange = SearchSheet.getRange("A1:A").getValues();
for (var i = 0, len = SearchRange.length; i < len; i++) {
if (SearchRange[i][0] == SearchValue) {
var MyFound = SearchSheet.getRange(i+1,2).getValue()
Browser.msgBox(MyFound)
}
}
}
To show a message no matter what, you'll want to update an existing variable if you find a match, and otherwise print a default message:
var message = "";
for(i = 0; i < array.length; ++i) {
if(array[i] == myValue) {
message += sheet.getRange(i, column).getValue();
break;
}
}
Browser.msgBox(message.length ? message : "Did not find the desired value");

can I check a regular expression on one array element?

I am trying to test if a specific element in an array is a range of characters, from a-z lowercase. what am I doing wrong? I am very new to coding (1 month in) and I am probably trying to do stuff thats too hard for me.
var array ["a","b","c"];
var myRegularExpression = /[a-z]/;
if (myRegularExpression.test(array[index])) {
//do stuff
}
To give you a working example as #Tushar mentioned:
var arr = ["a","b","c","123"];
var myRegularExpression = new RegExp("^[a-z]+$");
var matchCount = 0;
for (var index = 0; index < arr.length; index++) {
if (myRegularExpression.test(arr[index])) {
//do stuff
matchCount += 1;
}
}
document.getElementById("result").innerText = matchCount;
Number of elements matching the regex "^[a-z]+$":
<div id="result"></div>

Manipulate an Array with Javascript

I'm using the following script to validate a string.
var m;
var re = /([7]{1}[8,5]{1}[0-9]{2}[ ][0-9]{3}[ ][0-9]{3})[a-zA-Z0-9_.\- ]*([0-9]{6})/; // VW MQB
if ((m = re.exec(value)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
return m;
}
The result is an array. Now, I want to delete the spaces in all values. Futhermore I want to prepend one "0" (Zero) at the last value.
Is it possible without dissolve the array and afterwards to create an new array?
Thank you for your help!
Is it possible without dissolve the array and afterwards to create an new array?
Sure, just use a loop:
var n;
for (n = 0; n < m.length; ++n) {
m[n] = m[n].replace(/ /g, ""); // Remove spaces
}
m[m.length-1] = "0" + m[m.length-1]: // Add "0" before last entry

AS3: How to check if a value already exists in the Array before adding with FOR loop?

I believe is something simple but obviously not simple enough :). Any ideas how to check if a value already exists in the Array before adding the value using FOR loop?
I have this so far and it doesn't work as I want to because the Array can contain duplicate values!
var n:int = 5;
var cnt:int;
for (var i = 0; i < n; i++)
{
cnt = randomThief();
for (var a = 0; a < loto5.length; a++)
{
if (loto5[i] == cnt)
{
loto5[i] = cnt;
}
}
}
You can use the indexOf() method of the Array class to check if the value exists like this :
var index:int = loto5.indexOf(cnt);
indexOf() returns a -1, if the value doesn't exist. Here is an example of how to do a check :
if (loto5.indexOf(cnt) >= 0)
{
// do something
}
for (var a = 0; a < loto5.length; a++)
{
cnt = randomThief();
if (loto5.indexOf(cnt) == -1) //if cnt isn't in array do ...
{
trace (cnt+" is not in Array");
loto5[a] = cnt;
}
}
Works, simple and beauty :)

Resources