这是一个 typescript 函数来完成这项工作
有点混乱,但评论应该使它具有一定的可读性
只需将其复制粘贴到自动化选项卡下
function removeSpaces(text: string){
while (text[0] == ' ') {
text = text.substring(1, text.length);
}
return text;
}
function main(workbook: ExcelScript.Workbook) {
// Get the used range of the current worksheet.
const sheet = workbook.getActiveWorksheet();
const range = sheet.getUsedRange();
// create new worksheet
const name = "fixed"
let newSheet = workbook.getWorksheet(name)
if (newSheet){
newSheet.delete();
}
newSheet = workbook.addWorksheet(name)
newSheet.setTabColor("purple");
newSheet.activate();
const newTable = newSheet.addTable("A1:C1", true);
newTable.setName(name);
// cycle through selection
let rowi = 0;
const sy = range.getRowCount();
const selValues = range.getValues(); // array of array of values
for (let i=0; i<sy; i++){
// get row values
const row = selValues[i];
// get third value of row
let v = row[2].toString();
v = v.replace(/"/g, ''); // remove quotes
// split by comma
let elems = v.split(',')
elems.map((elem) => {
elem = removeSpaces(elem); // clean up spaces
newTable.addRow(rowi, [row[0], row[1], elem]); // add new row
rowi++;
});
}
}
之前:
之后: