我在创建自动脚本以将Excel列移动到数据透视表时遇到了很多麻烦。我在OSX 10.8.5上运行没有VB的Excel 2008。最终目标是将列数据字段转换为行数据字段。
tell application "Microsoft Excel"
open file "Users:dkstaff:Desktop:new.xlsx"
tell active sheet
tell used range
set rc to count of rows
end tell
set myRange to range ("D2:D" & rc)
set formula of myRange to "=A2&\":\"&B2&\":\"&C2"
# select sourceData
insert into range column 4
set header to range "D1"
set value of header to "KEY"
end tell
我对如何执行所选范围的数据透视表并将其放在新工作表上毫无头绪。
-- tell active workbook
# new sheet
-- set new sheet with name "New Table"
# var range of data
-- set source_data to range D2:L … of sheet "data"
# var sheet
-- set PTSheet to the sheet named "pivot Table"
# Implement a new Pivot Table from source range
-- set pivot_table to make new pivot table at PTSheet with properties source_data
tell active workbook
set newSheet to make new worksheet at end
tell newSheet
set name to "Pivot Table"
end tell
end tell
-- Failed attempt thus far...
set source_data to range ("D2:L" & rc) of sheet "data"
set PTSheet to the sheet named "Pivot Table"
set pivot_table to make new pivot table at PTSheet with properties source_data
##########
-- Another failed attempt
tell active sheet
set source_data to range ("D2:L" & rc) of sheet "data"
set PTSheet to the sheet named "Pivot Table"
set pivot_table to make new pivot table at PTSheet with properties source_data
end tell
end tell
结果:错误" Microsoft Excel出错:无法创建类数据透视表。"数字-2710从数据透视表到类
我无法解决此错误。
我在Excel中的工作流程是:选择列'键' - >数据透视表 - >多个合并范围 - >我将创建页面字段 - >范围:数据!D $ 2:$ L $ 1500 - >新表。
在新工作表上,双击总计总和并显示最终数据。还有一步,但我现在不想让我的工作流程复杂化。
如果有人有另一种方法可以自动从现有工作表创建数据透视表,请提及,否则,我希望继续学习AppleScript,因为我觉得它对其他任务也有帮助。