Skip to content

Instantly share code, notes, and snippets.

@luikore
Last active September 21, 2015 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luikore/95fb7aa6c846917e2399 to your computer and use it in GitHub Desktop.
Save luikore/95fb7aa6c846917e2399 to your computer and use it in GitHub Desktop.
collect cols
// run: cscript a.js
inFile = "C:\\Users\\z\\Desktop\\1.xlsx"
inSheet = 1
outFile = "C:\\Users\\z\\Desktop\\2.xlsx"
outSheet = 1
brandCol = 10 // J 列品牌
tradeCol = 12 // L 列行业
subCatCol = 13 // M 列子类
try {
Ax = new ActiveXObject("Excel.application");
} catch (err) {
WScript.Echo(err)
}
function readData(file, sheet) {
var wb = Ax.Workbooks.open(file)
wb.worksheets(sheet).select()
var rowCount = wb.worksheets(sheet).UsedRange.Cells.Rows.Count
var res = {}
for(var i = 2; i <= rowCount; i++) {
var brand = wb.ActiveSheet.Cells(i, 1).value
var trade = wb.ActiveSheet.Cells(i, 3).value
var subCat = wb.ActiveSheet.Cells(i, 4).value
res[brand] = [trade, subCat]
}
wb.Close()
return res
}
function writeFile(fname, sheet, data) {
// Ax.Visible = true
var wb = Ax.Workbooks.open(fname)
wb.worksheets(sheet).select()
var rowCount = wb.worksheets(sheet).UsedRange.Cells.Rows.Count
for (var i = 4; i <= rowCount; i++) {
var brand = wb.ActiveSheet.Cells(i, brandRow).value
var values = data[brand]
wb.ActiveSheet.Cells(i, tradeRow).value = values[0]
wb.ActiveSheet.Cells(i, subCatRow).value = values[1]
}
wb.SaveAs(fname.replace(/\.xslx$/, "-changed.xslx"))
wb.Close()
}
function work() {
// WScript.Echo("reading...")
var data = readData(inFile, inSheet)
// WScript.Echo("writing...")
writeFile(outFile, outSheet, data)
}
try {
work()
} catch (e) {
WScript.Echo("error" + e)
}
Ax.WorkBooks.Close()
Ax.Quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment