如何使用 beanshell 在 jmeter 中的 excel/csv 文件中的特定单元格写入数据?
How to write data at particular cell in excel/csv file in jmeter using beanshell?
我正在尝试使用 beanshell 将数据写入 excel 或 CSV 文件。但是我可以在 excel sheet 中写入数据,但无法在 CSV 文件中的特定单元格写入数据。
下面是代码。
var response = prev.getResponseDataAsString();
f = new FileOutputStream("C:/Users/adityak/Desktop/K/app.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(response);
f.close();
- CSV 文件没有 "cells",它是带有分隔符的纯文本文件,主要是逗号,因为 CSV 代表 comma-separated values
- 因为 JMeter 3.1 it is recommended to use Groovy for scripting
假设以上所有:
- 切换到JSR223 Test Element and Groovy语言
- 使用File.readLines()函数读取现有文件
- 通过String.split()函数
使用定界符将其拆分
- 将现有值替换为新值。
例如,如果您有这样的文件:
1,2,3
4,5,6
7,8,9
并想用 hello
替换 5
相关的 Groovy 代码类似于:
def csvFile = new File('/home/dtikhanski/Desktop/test.csv')
def lines = csvFile.readLines()
def secondLine = lines.get(1)
def entries = secondLine.split(",")
entries[1] = 'hello'
secondLine = entries.join(',')
lines.set(1, secondLine)
csvFile.withWriter{ out ->
lines.each {out.println it}
}
演示:
我正在尝试使用 beanshell 将数据写入 excel 或 CSV 文件。但是我可以在 excel sheet 中写入数据,但无法在 CSV 文件中的特定单元格写入数据。
下面是代码。
var response = prev.getResponseDataAsString();
f = new FileOutputStream("C:/Users/adityak/Desktop/K/app.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(response);
f.close();
- CSV 文件没有 "cells",它是带有分隔符的纯文本文件,主要是逗号,因为 CSV 代表 comma-separated values
- 因为 JMeter 3.1 it is recommended to use Groovy for scripting
假设以上所有:
- 切换到JSR223 Test Element and Groovy语言
- 使用File.readLines()函数读取现有文件
- 通过String.split()函数 使用定界符将其拆分
- 将现有值替换为新值。
例如,如果您有这样的文件:
1,2,3
4,5,6
7,8,9
并想用 hello
替换 5
相关的 Groovy 代码类似于:
def csvFile = new File('/home/dtikhanski/Desktop/test.csv')
def lines = csvFile.readLines()
def secondLine = lines.get(1)
def entries = secondLine.split(",")
entries[1] = 'hello'
secondLine = entries.join(',')
lines.set(1, secondLine)
csvFile.withWriter{ out ->
lines.each {out.println it}
}
演示: