所以我正在使用AppleScript编写脚本,需要将内容写入CSV的列中。我已经从CSV中读取了数据,并且有关如何写入CSV的代码,但它根本不写入。我想在第22列上写入。目前,我可以从该列中读取,但是我不确定如何编辑该列。或者是否有一种方法可以编辑Excel中特定的单元格,例如A3,以显示“是”。以下是我的代码。
set theFile to (choose file)
set f to read theFile
-- break the file into paragraphs (c.f. rows)
repeat with row in (paragraphs of f)
-- parse the row into comma-delimited fields
set fields to parseCSV(row as text)
-- now you have your data:
set verifyAccountStatus to item 22 of fields
set theResult to writeTo(verifyAccountStatus, "Yes", text, true)
end repeat
on parseCSV(theText)
set {od, my text item delimiters} to {my text item delimiters, ","}
set parsedText to text items of theText
set my text item delimiters to od
return parsedText
end parseCSV
on writeTo(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set openFile to open for access file targetFile with write permission
if apendData is false then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo
编辑: 由于某些原因,当我运行这个时它不起作用。
set theFile to (choose file)
set openFile to open for access theFile with write permission
do shell script "awk -F, '$1 ~ /Line 7/{$22=\"hello\"} 1' openFile"
close access openFile
它说无法打开文件openFile。