【问题标题】:Variable in a string字符串中的变量
【发布时间】:2014-03-04 13:45:34
【问题描述】:

我有以下脚本,但没有成功执行它而没有错误。我想将一个变量放入一个 URL 并 ping 该 URL。

#include <Excel.au3>

$sFilePath1 = "D:\Desktop\1.xlsx"
$oExcel = _ExcelBookOpen($sFilePath1, 0)

For $i = 1 To 2 ; Loop
    Local $username = _ExcelReadcell($oExcel, $i, 1) ;Reading created Excel
    Local $iPing = Ping("http://blogsearch.google.co.in/ping?url="$username"&btnG=Submit+Blog&hl=en", 2500)

    If $iPing Then ; If a value greater than 0 was returned then display the following message.
            MsgBox(0, "STATUS", "Ping Successful", 1)
    Else
        MsgBox(0, "STATUS", "ERROR", 1)
    EndIf
Next

【问题讨论】:

  • 您好,欢迎来到 SO。请添加更多详细信息,例如您遇到的错误和所需的输出。
  • 它必须从 excel 获取 url 并 ping 整个 url,而不是 "$username"。
  • 本地 $iPing = Ping("blogsearch.google.co.in/…", 2500) 本地 $iPing = Ping("blogsearch.google.co.in/…", 2500)
  • 你想达到什么目的? Ping 返回 的往返时间。 Url 指令(您的变量)与Ping() 无关;它只是将您的 ping 返回到blogsearch.google.co.in。 HTTP 和 ICMP 是不同的概念。 Related.

标签: string variables ping autoit


【解决方案1】:

您可以将变量连接到字符串

$id = 21622809
MsgBox(0, '', "http://example.com/user/" & $id & "/blah_blah_blah")

或启用ExpandVarStrings 选项并使用变量

$id = 21622809
Opt("ExpandVarStrings", 1)
MsgBox(0, "", "http://example.com/user/$id$/blah_blah_blah")

【讨论】:

    【解决方案2】:

    就像 Xenobiologist 已经在 cmets 中对这个问题所说的那样,这应该可以正常工作:

    #include <Excel.au3>
    
    $sFilePath1 = "D:\Desktop\1.xlsx"
    $oExcel = _ExcelBookOpen($sFilePath1, 0)
    For $i = 1 To 2 ;Loop
        Local $username = _ExcelReadcell($oExcel, $i, 1) ;Reading created Excel
        Local $iPing = Ping("http://blogsearch.google.co.in/ping?url="&$username&"&btnG=Submit+Blog&hl=en", 2500)
    
        If $iPing Then ; If a value greater than 0 was returned then display the following message.
            MsgBox(0, "STATUS", "Ping Successful" ,1)
        Else
            MsgBox(0, "STATUS", "ERROR" ,1)
        EndIf
    Next
    

    【讨论】:

      【解决方案3】:

      正如 Teifun2 所示,您只需在引号或双引号之间写下您的文本,然后在最后一个引号和变量之间加上“&”,这样:

      "I am writing this between double quotes at line # " & $i_Nbr & ' with simple quotes!' 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-21
        • 2013-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-21
        • 2016-05-30
        • 2020-09-26
        相关资源
        最近更新 更多