AppleScript – 如何将第一行用作文件名,第二行用作内容以创建json文件

huangapple go评论53阅读模式
英文:

AppleScript - How to take row 1 and use as file name and row 2 for content to create json files

问题

我有大量数据(367列,两行,但第二行包含大量内容!)要导出为用于Web显示的JSON文件。

我正在尝试自动化保存每个文件为.json文件的过程 - 理想情况下,它们应该以“Row 1 Value.json”命名,并包含第二行对应单元格的值。

我已经在AppleScript中完成了这一步,但似乎在尝试访问工作表时出现了问题 - 我收到一个错误消息,说“Can't get worksheet "HARDCODEJSON" of missing value.”,错误编号为-1728,来自<> "HARDCODEJSON" of missing value。

对于如何修改AppleScript的建议将不胜感激。我已经修改了文件路径,但只是如此。

英文:

I have a large amount of data (367 columns of two rows, but row 2 has a lot in it!) to export into json files for web display.

I'm trying to automate the process of saving each one as a .json file - ideally they need to be named Row 1 Value.json, and then contain the value of the corresponding cell in Row 2.

I've got this far in AppleScript, but it seems to fail at trying to get to the worksheet - I get an error saying "Can't get worksheet &quot;HARDCODEJSON&quot; of missing value." number -1728 from <<class XwSG>> "HARDCODEJSON" of missing value

Any suggestions as to how to modify the AppleScript would be well received. I've altered the file paths but that's it.


tell application &quot;Microsoft Excel&quot;
    set workbookName to POSIX file &quot;/Users/myname/Documents/exportedstreams/AllStreamsFlatFile.xlsx&quot;
    set sheetName to &quot;HARDCODEJSON&quot;
    set filePath to POSIX file &quot;/Users/myname/Documents/exportedstreams/&quot;
    
    -- Open the workbook file directly
    set workbookFile to open file workbookName
    set workbookObj to workbook of workbookFile
    
    -- Get the range of data from the specified sheet
    set sheetObj to get worksheet sheetName of workbookObj
    set dataRange to value of used range of sheetObj
    
    -- Loop through each column in the data range
    repeat with columnIndex from 1 to count of columns of dataRange
        set fileName to item columnIndex of item 1 of dataRange
        set fileContent to item columnIndex of item 2 of dataRange
        
        -- Save the content as a JSON file
        set fileRef to open for access ((filePath as text) &amp; fileName &amp; &quot;.json&quot;) with write permission
        set eof fileRef to 0
        write (fileContent as text) to fileRef
        close access fileRef
    end repeat
    
    -- Close the workbook file
    close workbookFile saving no
end tell

答案1

得分: 1

尝试这样做:

Excel有时在表示“路径+名称”时使用'name',所以我将您的变量更改得更明确。

set wbPath to POSIX file "/Users/myname/Documents/exportedstreams/AllStreamsFlatFile.xlsx"
set sheetName to "HARDCODEJSON"

tell application "Microsoft Excel"
    open wbPath -- née workbookName
    
    set wbName to name of workbook 1
    --> "AllStreamsFlatFile.xlsx"
    set wbObj to workbook wbName
    --> workbook "AllStreamsFlatFile.xlsx"
    
    set sheetObj to worksheet sheetName of wbObj
    set dataRange to value of used range of sheetObj
    
end tell

注:wbPath是文件URL,不是Excel对象,因此您不能询问Excel的属性(例如workbookworksheet),因为它没有任何属性。因此,会出现missing value错误。一旦文件被打开,成为Excel对象(工作簿),您就可以访问它的nameworksheet属性。

另外,再脚本的顶部,您使用了'posix file'与filePath变量。除非您真的需要使用文件引用,否则最好将其保留为字符串。在脚本的后面,您将其强制转换为文本,因此实际上没有必要这样做。

set filePath to "/Users/myname/Documents/exportedstreams/"

另一种方法:将电子表格作为工作簿打开

set wbPath to POSIX file "/Users/myname/Documents/exportedstreams/AllStreamsFlatFile.xlsx"
tell application "Microsoft Excel"
    set wbObj to open workbook workbook file name wbPath
    --> workbook "AllStreamsFlatFile.xlsx"
    
    -- 返回...
    wbObj
    --> workbook "AllStreamsFlatFile.xlsx" of application "Microsoft Excel"
end tell
英文:

Try this:

Excel sometimes uses 'name' when it means 'path+name' so I changed your variable to be more explicit.

set wbPath to POSIX file &quot;/Users/myname/Documents/exportedstreams/AllStreamsFlatFile.xlsx&quot;
set sheetName to &quot;HARDCODEJSON&quot;

tell application &quot;Microsoft Excel&quot;
	open wbPath -- n&#233;e workbookName
	
	set wbName to name of workbook 1
	--&gt; &quot;AllStreamsFlatFile.xlsx&quot;
	set wbObj to workbook wbName
	--&gt; workbook &quot;AllStreamsFlatFile.xlsx&quot;
	
	set sheetObj to worksheet sheetName of wbObj
	set dataRange to value of used range of sheetObj
	
end tell

Notes: wbPath is a file url, not an excel object — you can't ask excel for its properties (e.g. workbook, worksheet) because it doesn't have any. Hence, your missing value error. Once the file has been opened, and thus become an excel object (a workbook), you can access its name and worksheet properties.

On another note, near the top of your script, you use 'posix file' with your filePath variable. I'd leave it as a string unless you actually need to use the file reference. Later in the script you coerce it to text anyway so there really isn't a point.

set filePath to &quot;/Users/myname/Documents/exportedstreams/&quot;

Alternate approach: open the spreadsheet as a workbook

set wbPath to POSIX file &quot;/Users/myname/Documents/exportedstreams/AllStreamsFlatFile.xlsx&quot;
tell application &quot;Microsoft Excel&quot;
	set wbObj to open workbook workbook file name wbPath
	--&gt; workbook &quot;AllStreamsFlatFile.xlsx&quot;
	
	-- returns…
	wbObj
	--&gt; workbook &quot;AllStreamsFlatFile.xlsx&quot; of application &quot;Microsoft Excel&quot;
end tell

huangapple
  • 本文由 发表于 2023年7月18日 00:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定