有没有一种方法可以从多个 URL 生成 Web 查询而无需对 Excel 中的值进行硬编码?

Is there a way to generate a Web Query from multiple URL's without hardcoding the value in Excel?

我正在尝试使我从我公司的 git 存储库下载 JSON 数据并将其显示在 excel 中的过程自动化。在这个过程中我需要做的一件事是从一个网址创建一个新的查询,这需要能够根据脚本用户想要的 git 存储库进行更改。我选择录制一个宏,然后将网址粘贴到 URL 文本框中,效果很好。问题是宏不记录从剪贴板粘贴的过程,它只识别已添加的文本,认为我有意要将一个特定的网址硬编码到宏中而不是粘贴行为,这就是我真正想要的。有没有一种好方法可以避免将给定的 URL 硬编码到 Excel 中的“新建查询”->“从 Web”选项卡中,并且能够在调整 URL 值的同时保留宏?

这是我的 VBA 代码,删除了 URL 和 Token 值:

   Sub JSONtoEXCEL()
'
' JSONtoEXCEL Macro
'
' Keyboard Shortcut: Ctrl+Shift+J
'
    'Range( _
     '   "Table4[[#Headers],[http://URL]]" _
      '  ).Select
    ActiveCell.FormulaR1C1 = _
        "http://URL"
    Range("B1").Select
    ActiveWorkbook.Queries.Add Name:= _
        "issues?state=open&access_token=token#", _
        Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "Source = Json.Document(Web.Contents(""" & Sheets("Sheet1").Range("$B").Value & """))" & Chr(13) & "" & Chr(10) & "    #""Converted to Table"" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error)," & Chr(13) & "" & Chr(10) & "    #""Expanded Column1"" = Table.ExpandRecordColumn(#""Converte" & _
        "d to Table"", ""Column1"", {""id"", ""url"", ""html_url"", ""number"", ""user"", ""original_author"", ""original_author_id"", ""title"", ""body"", ""ref"", ""labels"", ""milestone"", ""assignee"", ""assignees"", ""state"", ""is_locked"", ""comments"", ""created_at"", ""updated_at"", ""closed_at"", ""due_date"", ""pull_request"", ""repository""}, {""id"", ""url"", """ & _
        "html_url"", ""number"", ""user"", ""original_author"", ""original_author_id"", ""title"", ""body"", ""ref"", ""labels"", ""milestone"", ""assignee"", ""assignees"", ""state"", ""is_locked"", ""comments"", ""created_at"", ""updated_at"", ""closed_at"", ""due_date"", ""pull_request"", ""repository""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Expanded Column1"""
    Sheets.Add After:=ActiveSheet
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""issues?state=open&access_token=token#" _
        , "29f0ca0d90"";Extended Properties="""""), Destination:=Range("$A")). _
        QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array( _
        "SELECT * FROM [issues?state=open&access_token=token#]" _
        )
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = False
        .ListObject.DisplayName = _
        "issues_state_open_access_token_token#"
        If Index = ctr Then
        Else
            .Refresh BackgroundQuery:=False
        End If
        
    End With
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False 'line that messes up
    Range("issues_state_open_access_token_token#" _
        ).Select
    Range("C6").Activate
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub

此代码是通过录制宏并通过将 JSON 数据加载到 excel 的过程生成的。我试图用包含 URL 的单元格替换字符串 URL。我收到错误:“运行-time error: '1004' 无法识别名称 'Source'。确保其拼写正确。

当我用文字 URL 替换对包含 URL 的单元格的引用时,此错误消失。我也尝试过改变引用单元格的方式(绝对寻址、使用“&”将坐标作为引用传递、删除引号等)。

编辑:我用最新的更改替换了代码并尝试修复格式问题。 令牌编号已替换为token#,URL已替换为URL

因为 Range("B1").Value 在引用的文本中,所以它不会被插入到它的值中。

改变这个


& "Source = Json.Document(Web.Contents(Range("B1").Value)) & 

to 

& "Source = Json.Document(Web.Contents(""" & Range("B1").Value & """))," &

更正后,您的 Power Query 应如下所示

let
Source = Json.Document(Web.Contents("http://URL")),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "url", "html_url", "number", "user", "original_author", "original_author_id", "title", "body", "ref", "labels", "milestone", "assignee", "assignees", "state", "is_locked", "comments", "created_at", "updated_at", "closed_at", "due_date", "pull_request", "repository"}, {"id", "url", "html_url", "number", "user", "original_author", "original_author_id", "title", "body", "ref", "labels", "milestone", "assignee", "assignees", "state", "is_locked", "comments", "created_at", "updated_at", "closed_at", "due_date", "pull_request", "repository"})
in
    #"Expanded Column1"

这是最终为我工作的代码:

Sub JsonToExcel()
'
' JsonToExcel Macro
'
' Keyboard Shortcut: Ctrl+Shift+J
'
    ActiveWorkbook.Queries.Add Name:= _
        "issues?state=open&access_token=", _
        Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Json.Document(Web.Contents(""" & Sheets("Sheet1").Range("$B").Value & """))," & Chr(13) & "" & Chr(10) & "    #""Converted to Table"" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error)," & Chr(13) & "" & Chr(10) & "    #""Expanded Column1"" = Table.ExpandRecordColumn(#""Converted to Tab" & _
        "le"", ""Column1"", {""id"", ""url"", ""html_url"", ""number"", ""user"", ""original_author"", ""original_author_id"", ""title"", ""body"", ""ref"", ""labels"", ""milestone"", ""assignee"", ""assignees"", ""state"", ""is_locked"", ""comments"", ""created_at"", ""updated_at"", ""closed_at"", ""due_date"", ""pull_request"", ""repository""}, {""id"", ""url"", ""html_url" & _
        """, ""number"", ""user"", ""original_author"", ""original_author_id"", ""title"", ""body"", ""ref"", ""labels"", ""milestone"", ""assignee"", ""assignees"", ""state"", ""is_locked"", ""comments"", ""created_at"", ""updated_at"", ""closed_at"", ""due_date"", ""pull_request"", ""repository""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Expanded Column1"""
    Sheets.Add After:=ActiveSheet
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""issues?state=open&access_token=" _
        , "29f0ca0d90"";Extended Properties="""""), Destination:=Range("$A")). _
        QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array( _
        "SELECT * FROM [issues?state=open&access_token=]" _
        )
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = False
        .ListObject.DisplayName = _
        "issues_state_open_access_token_"
        .Refresh BackgroundQuery:=False
    End With
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
    Range("issues_state_open_access_token_" _
        ).Select
    Range("B2").Activate
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub

令牌编号有意留空