Google 应用脚本将 CSV 导出到客户分隔符
Google App Script export CSV to a customer Delimiter
我有这个脚本可以将我的 google 工作表导出到 CSV 文件。我想导出一个带有客户分隔标签“|”的 CSV。
var pasteDataRequest = Sheets.newPasteDataRequest();
pasteDataRequest.coordinate = {
sheetId: SpreadsheetApp.getActiveSheet().getSheetId(),
rowIndex: 0,
columnIndex: 0
};
pasteDataRequest.data = data;
pasteDataRequest.type = SpreadsheetApp.CopyPasteType.PASTE_VALUES;
pasteDataRequest.delimiter = '|';
我有完整的剧本here。
如果我以当前方式导出,它仍然由逗号“,”分隔。
如何使用“|”将我的数据导出为 csv 格式?
如何输出类似于 CSV 但带有自定义分隔符的文本文件
这是一种非常简单的方法,可以将您的文件输出为类似 CSV 的文件类型,但不是用 ,
分隔,而是用 |
或您想要的任何分隔符分隔。
function createCsvContent(values, delimiter) {
// This converts the values 2D array into a simple array of strings delimited by the delimiter
const stringArray = values.map(row => row.join(delimiter))
// Then it joins all the strings with newlines
const output = stringArray.join("\n")
return output
}
function createCsv() {
// Get all the values from the sheet as a 2D array
const file = SpreadsheetApp.getActive();
const sheet = file.getSheetByName("Sheet1");
const range = sheet.getDataRange();
const values = range.getValues();
// call the function to create the csv-like content
const csvContent = createCsvContent(values, "|")
// create the file in drive
DriveApp.createFile('csvFile', csvContent, MimeType.PLAIN_TEXT)
}
请注意,最后一行将文件创建为纯文本文件。我本来打算制作此 CSV,但由于它不再以逗号分隔,因此不能再归类为 CSV 文件。虽然如果你改变它,它仍然会工作。
主要创建发生在 createCsvContent
函数中。它使用数组的 map
方法将从电子表格中获得的二维数组转换为简单的字符串数组。
发件人:
[
[1,2,3],
[4,5,6],
[7,8,9],
]
收件人:
[
"1|2|3",
"4|5|6",
"7|8|9",
]
然后最后在主数组上使用另一个 join
将其转换为:
"1|2|3\n4|5|6\n7|8|9"
其中的\n
表示“换行”,最后会这样渲染:
1|2|3
4|5|6
7|8|9
然后是调用 DriveApp.createFile
的情况,其中包含所需文件的名称、createCsvContent
函数生成的内容以及 mime 类型。
编辑
每次替换同一个文件,而不是:
// create the file in drive
DriveApp.createFile('csvFile', csvContent, MimeType.PLAIN_TEXT)
您需要获取“csv”的文件 ID 并使用 getFileById
和 setContent
:
DriveApp.getFileById("[FILE_ID]").setContent(csvContent)
编辑 2
要获取驱动器中任何文件的 ID,您首先要转到驱动器并右键单击您想要的文件。点击“获取Link”,然后在这里你会找到ID:
参考资料
我有这个脚本可以将我的 google 工作表导出到 CSV 文件。我想导出一个带有客户分隔标签“|”的 CSV。
var pasteDataRequest = Sheets.newPasteDataRequest();
pasteDataRequest.coordinate = {
sheetId: SpreadsheetApp.getActiveSheet().getSheetId(),
rowIndex: 0,
columnIndex: 0
};
pasteDataRequest.data = data;
pasteDataRequest.type = SpreadsheetApp.CopyPasteType.PASTE_VALUES;
pasteDataRequest.delimiter = '|';
我有完整的剧本here。
如果我以当前方式导出,它仍然由逗号“,”分隔。
如何使用“|”将我的数据导出为 csv 格式?
如何输出类似于 CSV 但带有自定义分隔符的文本文件
这是一种非常简单的方法,可以将您的文件输出为类似 CSV 的文件类型,但不是用 ,
分隔,而是用 |
或您想要的任何分隔符分隔。
function createCsvContent(values, delimiter) {
// This converts the values 2D array into a simple array of strings delimited by the delimiter
const stringArray = values.map(row => row.join(delimiter))
// Then it joins all the strings with newlines
const output = stringArray.join("\n")
return output
}
function createCsv() {
// Get all the values from the sheet as a 2D array
const file = SpreadsheetApp.getActive();
const sheet = file.getSheetByName("Sheet1");
const range = sheet.getDataRange();
const values = range.getValues();
// call the function to create the csv-like content
const csvContent = createCsvContent(values, "|")
// create the file in drive
DriveApp.createFile('csvFile', csvContent, MimeType.PLAIN_TEXT)
}
请注意,最后一行将文件创建为纯文本文件。我本来打算制作此 CSV,但由于它不再以逗号分隔,因此不能再归类为 CSV 文件。虽然如果你改变它,它仍然会工作。
主要创建发生在 createCsvContent
函数中。它使用数组的 map
方法将从电子表格中获得的二维数组转换为简单的字符串数组。
发件人:
[
[1,2,3],
[4,5,6],
[7,8,9],
]
收件人:
[
"1|2|3",
"4|5|6",
"7|8|9",
]
然后最后在主数组上使用另一个 join
将其转换为:
"1|2|3\n4|5|6\n7|8|9"
其中的\n
表示“换行”,最后会这样渲染:
1|2|3
4|5|6
7|8|9
然后是调用 DriveApp.createFile
的情况,其中包含所需文件的名称、createCsvContent
函数生成的内容以及 mime 类型。
编辑
每次替换同一个文件,而不是:
// create the file in drive
DriveApp.createFile('csvFile', csvContent, MimeType.PLAIN_TEXT)
您需要获取“csv”的文件 ID 并使用 getFileById
和 setContent
:
DriveApp.getFileById("[FILE_ID]").setContent(csvContent)
编辑 2
要获取驱动器中任何文件的 ID,您首先要转到驱动器并右键单击您想要的文件。点击“获取Link”,然后在这里你会找到ID: