This AutoIt script counts number of words; characters (with and without spaces); lines; and estimated speaking time (assuming two words per second) for text selected by the user.
However, if the starting position is 0 for the input string (at the upper left-hand corner of the top of the file) the method returns 0 for everything, even when the main function works perfectly.
I cannot figure out why.
$input_str = GUICtrlRead($Textbox)
$selpos = _GUICtrlEdit_GetSel($Textbox)
MsgBox($MB_OK, $selpos[0], $selpos[1])
$selstring = StringMid($input_str, $selpos[0], ($selpos[1] - $selpos[0]))
$WordArray = StringRegExp($selstring, "[\s\.:;,]*([a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
$SingleQuotes = StringRegExp($selstring, "'", 3)
$Result = ""
$Seconds = (UBound($WordArray) - UBound($SingleQuotes)) / 2
If $Seconds >= 3600 Then
If $Seconds / 3600 >= 2 Then
$Result = $Result & Int($Seconds / 3600) & " hours "
Else
$Result = $Result & Int($Seconds / 3600) & " hour "
EndIf
EndIf
If Mod($Seconds, 3600) >= 60 Then
If $Seconds / 60 >= 2 Then
$Result = $Result & Int(Mod($Seconds, 3600) / 60) & " minutes "
Else
$Result = $Result & Int(Mod($Seconds, 3600) / 60) & " minute "
EndIf
EndIf
If Mod($Seconds, 60) > 0 Then
If Mod($Seconds, 60) >= 2 Then
$Result = $Result & Int(Mod($Seconds, 60)) & " seconds "
Else
$Result = $Result & Int(Mod($Seconds, 60)) & " second "
EndIf
EndIf
MsgBox($MB_OK, "Selection Properties", _
"Number of characters (with spaces): " & StringLen($selstring) & #CRLF & _
"Number of Characters (without spaces): " & StringLen(StringStripWS($selstring, 8)) & #CRLF & _
"Number of words: " & (UBound($WordArray) - UBound($SingleQuotes)) & #CRLF & _
"Number of lines: " & _GUICtrlEdit_GetLineCount($selstring) & #CRLF & _
"Estimated speaking time: " & $Result _
)
If $selpos[0] = 0 then StringMid() returns an empty string since your start is out of bounds (as first position for StringMid() is 1).
$sTest = "A sample test string"
MsgBox(0, '' , StringMid($sTest , 0 , 8))
MsgBox(0, '' , StringMid($sTest , 1 , 8))
Related
return
fn:concat (fn:string-join ((
"somevalue.1.",
"somevalue.2.",
"some val 3",
"some val4",
$somevariable), " "),
for $i in $loopvar
if ((fn:exists($loopvar)) and (fn:count($loopvar) > 1)) then
" where ( " || $loopvar[i] || " and "
else if(fn:exists($loopvar) and (fn:count($loopvar) > 0)) then
" where " || $loopvar[i]
else() )
I m trying the above code but its giving me error at the fn:concat line. Can anyone help here?
The fn:concat() function takes any number of single strings and not a sequence.
The fn:string-join() function concatenates sequences.
So, one solution would be to include the for loop in the sequence passed to string-join(), as in:
fn:concat(fn:string-join((
"somevalue.1.",
"somevalue.2.",
"some val 3",
"some val4",
$somevariable,
for $i in $loopvar
return
if ((fn:exists($loopvar)) and (fn:count($loopvar) > 1)) then
"where ( " || $loopvar[i] || " and "
else if(fn:exists($loopvar) and (fn:count($loopvar) > 0)) then
"where " || $loopvar[i]
else() )
), " ")
Hoping that helps,
I have a table with for which the column "CODE" has values like this:
FTRA2
BRB92
RBRB4
XYZ
SXM4
RBRB4
NLDR
XYZ
FTRA2
POEU
FTRA2
I currently have this formula
="[ Unique values " & DISTINCTCOUNT(MyTable[CODE]) & "]
" & CONCATENATEX(DISTINCT(MyTable[CODE]), MyTable[CODE] ,", ")
that outputs this:
[ Unique values 7 ]
FTRA2, BRB92, RBRB4, XYZ, SXM4, NLDR, POEU
I would like to show all the unique values and their count (except those with the string "XYZ") and below show how many "XYZ" values are, like this:
[ Unique values 6 ]
FTRA2, BRB92, RBRB4, SXM4, NLDR, POEU
[2 XYZ values]
In this case there are 2 "XYZ" values, but could be zero XYZ values too.
I'm using Excel 2016.
How can I do this? Thanks in advance.
UPDATE1
I get this error tryng Joe's solution.
UPDATE2
Joe, I was able to make work your first part modifying like this:
= VAR ExcludeValue = "XYZ"
RETURN
CALCULATE(
"[ Unique values " & DISTINCTCOUNT(MyTable[Code]) & " ]"
" & CONCATENATEX(DISTINCT(MyTable[Code]), [Code], ", ")
, MyTable[Code] <> ExcludeValue
)
But when I add the second part it says this error
This formula is invalid or incomplete: 'Calculation error in
measure 'MyTable[Code]: The function COUNT takes an argument
that evaluates to numbers or dates and cannot work with values
of type String.'.
I also removed the UNICHAR since doesn't work on Excel.
UPDATE3
Joe's solution it works correctly after I modified the COUNT(MyTable[Code]) to COUNTROWS(MyTable)
The final solution looks like this.
=VAR ExcludeValue = "XYZ"
RETURN
CALCULATE(
"
[ Unique values " & DISTINCTCOUNT(MyTable[Code]) & " ]
" & CONCATENATEX(DISTINCT(MyTable[Code]), [Code], ", ")
, MyTable[Code] <> ExcludeValue
) & "
" & CALCULATE(
"[" & COUNTROWS(MyTable) & " " & ExcludeValue & " values]"
, MyTable[Code] = ExcludeValue
) & "
"
Update4
Print nothing when there is no "XYZ" values works with your IF() addition. I've tried to follow your logic to do the same when there is no values at all. I added an
IF() to count if MyTable[Code] <> ExcludeValue is greater than 0 and if true do original CALCULATE, if not BLANK() but doesnt work.
CountLabel =
VAR ExcludeValue = "XYZ"
RETURN
IF(
CALCULATE(COUNTROWS(MyTable), MyTable[Code] <> ExcludeValue) > 0,
CALCULATE(
"[ Unique values " & DISTINCTCOUNT(MyTable[Code]) & " ]"
& UNICHAR(10) &
CONCATENATEX(DISTINCT(MyTable[Code]), [Code], ", ")
, MyTable[Code] <> ExcludeValue
),
BLANK()
)
& IF(
CALCULATE(COUNTROWS(MyTable), MyTable[Code] = ExcludeValue) > 0,
UNICHAR(10) & " " & UNICHAR(10) &
CALCULATE(
"[" & COUNTROWS(MyTable) & " " & ExcludeValue & " values]"
, MyTable[Code] = ExcludeValue
),
BLANK()
)
FINAL UPDATE
This is the final formula that works as expected. Thanks to Joe's help in this case.
=VAR ExcludeValue = "XYZ"
RETURN
IF(
CALCULATE(DISTINCTCOUNT(MyTable[Code]), MyTable[Code] <> ExcludeValue) > 0 &&
MyTable[Count of Code]>0,
CALCULATE(
"
[ Unique values " & DISTINCTCOUNT(MyTable[Code]) & " ]
" & CONCATENATEX(DISTINCT(MyTable[Code]), [Code], ", ")
, MyTable[Code] <> ExcludeValue
),
BLANK()
)
&
IF(
CALCULATE(DISTINCTCOUNT(MyTable[Code]), MyTable[Code] <> ExcludeValue) > 0 &&
CALCULATE(COUNTROWS(MyTable), MyTable[Code] = ExcludeValue) > 0,
"
" &
BLANK()
)
& IF(
CALCULATE(COUNTROWS(MyTable), MyTable[Code] = ExcludeValue) > 0,
CALCULATE(
"[" & COUNTROWS(MyTable) & " " & ExcludeValue & " values]"
, MyTable[Code] = ExcludeValue
),
BLANK()
) & "
"
UPDATE: - Changed my formula from using COUNT to COUNTROWS based on feedback from OP.
UPDATE 2: - Add IF statement to formula to exclude excluded count when 0.
UPDATE 3: - Add IF statement to formula to exclude distinct count when 0.
I will say that I created this solution in Power BI, but Excel 2016 should have the same functionality when it comes to DAX (with minor tweaks).
I created a measure with your formula, and simply wrapped each piece (the distinct count, and the repeated count) with a CALCULATE statement that is used to filter your MyTable down to the codes you care about.
I used a variable for the "XYZ" value in case that needs to be changed. Now you can simply change it in one place (at the beginning of the formula) and the rest of the formula will reflect that change.
I also used UNICHAR(10) to add the line breaks instead of counting on the new lines in the formula.
With the IF statements...
The first will check if the distinct count of items not equal to the specified value is greater than zero. If not, it won't show anything.
The second will check if the distinct count and the row count of the specified value are both greater than zero. If they are, it will add the line break.
The third will check if the row count of items equal to the specified value is greater than zero. If not, it won't show anything.
The final formula is:
CountLabel =
VAR ExcludeValue = "XYZ"
RETURN
IF(
CALCULATE(DISTINCTCOUNT(MyTable[Code]), MyTable[Code] <> ExcludeValue) > 0,
CALCULATE(
"[ Unique values " & DISTINCTCOUNT(MyTable[Code]) & " ]"
& UNICHAR(10) &
CONCATENATEX(DISTINCT(MyTable[Code]), [Code], ", ")
, MyTable[Code] <> ExcludeValue
),
BLANK()
)
&
IF(
CALCULATE(DISTINCTCOUNT(MyTable[Code]), MyTable[Code] <> ExcludeValue) > 0 &&
CALCULATE(COUNTROWS(MyTable), MyTable[Code] = ExcludeValue) > 0,
UNICHAR(10) & " " & UNICHAR(10),
BLANK()
)
& IF(
CALCULATE(COUNTROWS(MyTable), MyTable[Code] = ExcludeValue) > 0,
CALCULATE(
"[" & COUNTROWS(MyTable) & " " & ExcludeValue & " values]"
, MyTable[Code] = ExcludeValue
),
BLANK()
)
Here is what the result looks like (again, in Power BI).
Came up with something similar but slightly different using COUNTROWS instead of CALCULATE to filter the table for the unique item. Also I am just learning DAX so don't know if this is a "proper" way to do it, but it seems to work.
Measure =
VAR Exclusion = "XYZ"
RETURN
"[ Unique values " & COUNTROWS(FILTER(DISTINCT(MyTable[CODE]), [CODE] <> Exclusion)) & "]
" & CONCATENATEX(FILTER(DISTINCT(MyTable[CODE]), [CODE] <> Exclusion), [CODE] ,", ") &
"
[" & COUNTROWS(FILTER(MyTable, MyTable[CODE] = Exclusion))+0 & " " & Exclusion & " values]"
So I am in need of replacing string text with autoit using stringreplace but I need to randomize the output.
An example of what I need is
Stringreplace($string, "and", {also|as well})
My ultimate goal is to randomly replace the text with the following options based on the word "and" with also or as well
I wrote this a long time ago.
It will convert this
My name is {John|Peter|Mark}! {Regards|Cheers|Get lost}!
to something like this
My name is John! Cheers!
It works with line breaks also.
Func SpintaxToTXT($TextWithSpintax)
Dim $MSGMSG
Dim $lines
$lines = StringSplit($TextWithSpintax, #LF)
For $z = 1 To $lines[0]
If $z > 1 Then $MSGMSG &= #LF
$d = StringSplit($lines[$z], "{}")
For $i = 1 To $d[0]
$MSGSplit = StringSplit($d[$i], "|")
If #error Then
$MSGMSG &= $MSGSplit[1]
ContinueLoop
EndIf
$MSGMSG &= $MSGSplit[Random(1, $MSGSplit[0], 1)]
Next
Next
Return $MSGMSG
EndFunc ;==>SpintaxToTXT
Try this:
Global $options_A = StringSplit('also,as well,bla,blubb,that,this', ',', 2)
For $i = 0 To 20
ConsoleWrite($options_A[Random(0, UBound($options_A) - 1, 1)] & #CRLF)
Next
Here's one I made earlier. Works exactly the same as StringReplace except instead of taking a replacement string, it takes a function that returns the replacement string. The using Xenobiologist's array method, you get the desired result.
Local $sTest = "The cat and the dog and the rat."
ConsoleWrite(_StringReplaceCallback($sTest, "and", _MyCallback) & #LF)
Func _MyCallback($s)
Local Static $aOptions = StringSplit("also|as well", "|")
Return $aOptions[Random(1, $aOptions[0], 1)]
EndFunc ;==>_MyCallback
Func _StringReplaceCallback($sString, $sFind, $funcReplace, $iOccurence = 0, $iCaseSense = 0)
Local $sRet = ""
Local $iDir = 1
Local $iPos = 1
If $iOccurence < 0 Then
$iDir = -1
$iPos = StringLen($sString)
EndIf
If $iOccurence = 0 Then $iOccurence = $iDir * StringLen($sString)
While 1
$i = StringInStr($sString, $sFind, $iCaseSense, $iDir, $iPos)
If $iDir > 0 Then
If Not $i Or Not $iOccurence Then
$sRet &= StringMid($sString, $iPos)
ExitLoop
EndIf
$sRet &= StringMid($sString, $iPos, $i - $iPos) & $funcReplace($sFind)
$iPos = $i + StringLen($sFind)
Else
If Not $i Or Not $iOccurence Then
$sRet = StringMid($sString, 1, $iPos) & $sRet
ExitLoop
EndIf
$sRet = $funcReplace($sFind) & StringMid($sString, $i + StringLen($sFind), $iPos - $i - StringLen($sFind) + 1) & $sRet
$iPos = $i - 1
EndIf
If $iOccurence <> 0 Then $iOccurence -= $iDir
WEnd
Return $sRet
EndFunc ;==>_StringReplaceCallback
It's probably worth noting that there is actually a simpler solution for this, provided the replacement strings don't ever include "and" (or else you'll have an infinite loop), that just replaces one instance at a time:
Local $sString = "The cat and the dog and the rat."
Local $aOptions = StringSplit("also|as well", "|")
Do
$sString = StringReplace($sString, "and", $aOptions[Random(1, $aOptions[0], 1)], 1)
Until Not #extended
ConsoleWrite($sString & #LF)
I'm in the process of making a small program to tell me on the fly how many employees are currently punched in via AvayaCMS Reporting.
Right now, the way that it is setup is that I have an Avaya script to take a quick report and export it in a CSV that is used by my Autoit script.
In terms of debugging it, I feel like I missing something and need another set of eyes.
launching the Staffing.au3 triggers the CSV script I'm using against the report. Even having the exact same data my message box still reports "0"
#include <Array.au3>
#include <CSV.au3>
$i_AgentCount = AvayaData(#ScriptDir & '\Report.csv', #ScriptDir & '\Name List.csv')
MsgBox(0x40, "", $i_AgentCount)
Func AvayaData($s_ReportSource, $s_NameList)
$av_LiveData = _ParseCSV($s_ReportSource)
If #error Then Return -1
$av_NameList = _ParseCSV($s_NameList)
If #error Then Return -1
Local $i_AgentCount = 0
For $i_RowCnt = 1 To (UBound($av_LiveData, 1) - 1) Step +1
For $i_AgtLst = 1 To (UBound($av_NameList) - 1) Step +1
If StringStripWS($av_LiveData[$i_RowCnt][1], 3) = StringStripWS($av_NameList[$i_AgtLst][0], 3) Then
$i_AgentCount += 1
EndIf
Next
Next
;Return the Agent Count
Return $i_AgentCount
EndFunc
Name List.csv
Agent Name
"Doe, Jane"
"Doe, John"
Report.csv
,Agent Name,Login ID,Extn,AUX Reason,State,Split/Skill,Time,VDN Name
5,"Doe, John",5930001,1000001,7,AUXOUT,999,51:32:00,
2,"Doe, Jane",5930002,1000002,7,AUXOUT,999,52:32:00,
Tested! It works well with the files supplied (copied them and names them as in your script)
please note the followings
_ParseCsv() has been rewritten add parameters like $Dchar as delimeter etc. (see script)
Note how I locate the names in the file (you can easily add feature to extend search but it won't be becessary)
msgboxes are for explanatory purposes; comment them out if not need them anymore
array.au3 is not needed
and the code:
$i_AgentCount = AvayaData(#ScriptDir & '\Report.csv', #ScriptDir & '\Name List.csv')
MsgBox(0x40, "", $i_AgentCount)
Func AvayaData($s_ReportSource, $s_NameList)
$av_LiveData = _ParseCSV($s_ReportSource,",","oops...",True)
If #error Then Return -1
$av_NameList = _ParseCSV($s_NameList,",","oops: 2",True)
If #error Then Return -1
MsgBox(0,default,"$av_NameList (Number of lines) -> " & $av_NameList[0][0])
MsgBox(0,default,"$av_NameList (Number of data in each line) -> " & $av_NameList[0][1])
MsgBox(0,default,"$av_LiveData (Number of lines) -> " & $av_LiveData[0][0])
MsgBox(0,default,"$av_LiveData (Number of data in each line) -> " & $av_LiveData[0][1])
Local $i_AgentCount = 0
for $i = 1 to $av_NameList[0][0] Step 1
For $j= 1 To $av_LiveData[0][0] Step 1
;we can have names from $av_NameList as well from $av_LiveData but in Live Data 2nd abd 3rd values are the names
MsgBox(0,default,$av_NameList[$i][1] & $av_NameList[$i][2] & " <------------> " & $av_LiveData[$j][2] & $av_LiveData[$j][3])
;~ let's compare them if matched with any of the liveData lines
If StringCompare($av_NameList[$i][1] & $av_NameList[$i][2],$av_LiveData[$j][2] & $av_LiveData[$j][3]) == 0 Then
$i_AgentCount += 1
MsgBox(0,default,"Match found: counter: " & $i_AgentCount)
EndIf
Next
Next
;Return the Agent Count
Return $i_AgentCount
EndFunc
Func _ParseCSV($f,$Dchar,$error,$skip)
Local $array[500][500]
Local $line = ""
$i = 0
$file = FileOpen($f,0)
If $file = -1 Then
MsgBox(0, "Error", $error)
Return False
EndIf
;skip 1st line (since it is the header)
If $skip Then $line = FileReadLine($file)
While 1
$i = $i + 1
Local $line = FileReadLine($file)
If #error = -1 Then ExitLoop
;skip 1st line
$row_array = StringSplit($line,$Dchar)
If $i == 1 Then $row_size = UBound($row_array)
If $row_size <> UBound($row_array) Then MsgBox(0, "Error", "Row: " & $i & " has different size ")
$row_size = UBound($row_array)
$array = _arrayAdd_2d($array,$i,$row_array,$row_size)
WEnd
FileClose($file)
$array[0][0] = $i-1 ;stores number of lines
$array[0][1] = $row_size -1 ; stores number of data in a row (data corresponding to index 0 is the number of data in a row actually that's way the -1)
Return $array
EndFunc
Func _arrayAdd_2d($array,$inwhich,$row_array,$row_size)
For $i=1 To $row_size -1 Step 1
$array[$inwhich][$i] = $row_array[$i]
Next
Return $array
EndFunc
#region ;************ Includes ************
#include "csv.au3"
#include <Array.au3>
#endregion ;************ Includes ************
$i_AgentCount = AvayaData(#ScriptDir & '\Report.csv', #ScriptDir & '\Name List.csv')
MsgBox(0x40, "", $i_AgentCount)
Func AvayaData($s_ReportSource, $s_NameList)
Local $i_AgentCount = 0
Local $s = FileRead($s_NameList)
Local $loggedInNames = FileRead($s_ReportSource)
$av_NameList = _ParseCSV($s_NameList)
If #error Then Return -1
;~ _ArrayDisplay($av_NameList)
ConsoleWrite($s & #CRLF)
For $i = 1 To UBound($av_NameList) - 1
ConsoleWrite($i & " " & $av_NameList[$i][0] & #CRLF)
If StringInStr($s, $av_NameList[$i][0]) <> 0 Then
$i_AgentCount += 1
EndIf
Next
Return $i_AgentCount
EndFunc ;==>AvayaData
Does anyone have a relative date/time from now to a natural/human for classic ASP function in VBScript? This is like Twitter.
Examples:
Less than 1 minute ago
About 5 minutes ago
About an hour ago
About 3 hours ago
Yesterday
Wednesday
etc.
This is the one I use. Pretty certain I just ripped it from Jeff's example that he used for this site.
Yes, yes I did: How can I calculate relative time in C#?
Function RelativeTime(dt)
Dim t_SECOND : t_SECOND = 1
Dim t_MINUTE : t_MINUTE = 60 * t_SECOND
Dim t_HOUR : t_HOUR = 60 * t_MINUTE
Dim t_DAY : t_DAY = 24 * t_HOUR
Dim t_MONTH : t_MONTH = 30 * t_DAY
Dim delta : delta = DateDiff("s", dt, Now)
Dim strTime : strTime = ""
If (delta < 1 * t_MINUTE) Then
If delta = 0 Then
strTime = "just now"
ElseIf delta = 1 Then
strTime = "one second ago"
Else
strTime = delta & " seconds ago"
End If
ElseIf (delta < 2 * t_MINUTE) Then
strTime = "a minute ago"
ElseIf (delta < 50 * t_MINUTE) Then
strTime = Max(Round(delta / t_MINUTE), 2) & " minutes ago"
ElseIf (delta < 90 * t_MINUTE) Then
strTime = "an hour ago"
ElseIf (delta < 24 * t_HOUR) Then
strTime = Round(delta / t_HOUR) & " hours ago"
ElseIf (delta < 48 * t_HOUR) Then
strTime = "yesterday"
ElseIf (delta < 30 * t_DAY) Then
strTime = Round(delta / t_DAY) & " days ago"
ElseIf (delta < 12 * t_MONTH) Then
Dim months
months = Round(delta / t_MONTH)
If months <= 1 Then
strTime = "one month ago"
Else
strTime = months & " months ago"
End If
Else
Dim years : years = Round((delta / t_DAY) / 365)
If years <= 1 Then
strTime = "one year ago"
Else
strTime = years & " years ago"
End If
End If
RelativeTime = strTime
End Function
taken from ajaxed
'here comes some global helpers...
public function sayDate(dat, mode, relativNotation)
if not isDate(dat) then
sayDate = "unknown"
exit function
end if
if relativNotation then
diff = dateDiff("s", dat, now())
if diff <= 10 and diff >= 0 then
sayDate = "Just now" : exit function
elseif diff < 60 and diff >= 0 then
sayDate = diff & " seconds ago" : exit function
elseif diff = 60 and diff >= 0 then
sayDate = diff & " minute ago" : exit function
elseif diff <= 1800 and diff >= 0 then
sayDate = int(diff / 60) & " minutes ago" : exit function
elseif diff < 86400 and diff >= 0 then
sayDate = plural(int(diff / 60 / 60), "hour", empty) & " ago"
else
if datevalue(dat) = date() then
sayDate = "Today"
elseif dateValue(dat) = dateAdd("d", 1, date()) then
sayDate = "Tomorrow"
elseif dateValue(dat) = dateAdd("d", -1, date()) then
sayDate = "Yesterday"
end if
end if
end if
if relativNotation and lCase(mode) = "datetime" and isEmpty(sayDate) then
diff = dateDiff("d", dat, now())
sayDate = plural(diff, "day", empty) & " ago"
exit function
end if
if isEmpty(sayDate) then
sayDate = day(dat) & ". " & monthname(month(dat), true)
if year(dat) <> year(now()) then sayDate = sayDate & " " & year(dat)
end if
if lCase(mode) <> "datetime" then exit function
if uBound(split(dat, " ")) <= 0 then exit function
'sayDate = sayDate & ", " & str.padLeft(hour(dat), 2, "0") & ":" & str.padLeft(minute(dat), 2, "0")
end function
public function plural(val, singularform, pluralform)
plural = singularform
if val <> 1 then plural = pluralform
if isEmpty(plural) then plural = singularform & "s"
plural = val & " " & plural
end function
I write my own function like this, could be found at http://asp.web.id/asp-classic-relative-date-function.html
it is used conversion asp date to unixtimestamp format and calculate the time margin. it is customizable you could also create relative date for upcoming date using this function.
DateAdd("n", -1, Now)
DateAdd("n", -5, Now)
DateAdd("h", -1, Now)
DateAdd("h", -3, Now)
DateAdd("d", -1, Date)
DateAdd("d", -1, Date)
Not sure about what you mean by Wednesday part.
Can you elaborate?