我有一个使用JDBC服务查询MySQL数据库的脚本。该脚本将运行查询,然后将结果打印在Google工作表中。
SQL表具有23列,列4中包含的数据包含多个用逗号分隔的字符串值。例如,“太阳能,柴油”。
此刻,查询将把所有内容从列中拉出,但我只需要逗号前的字符串部分,所以只需要“ Solar”。
我知道'SUBSTRING'可以完成这项工作,但是我似乎找不到在GAS中使用它的方法。这是下面的GAS脚本:
function ProjectSelect(){
//defining variables
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetTab = sheet.getSheetByName('Project Selector')
//defining inputs to SQL statement
//conditionals
var location = sheetTab.getRange(3,2).getValue();
var country = sheetTab.getRange(5,2).getValue();
var opStatus = sheetTab.getRange(7,2).getValue();
var gridStatus = sheetTab.getRange(9,2).getValue();
var fuel = sheetTab.getRange(11,2).getValue();
var technology = sheetTab.getRange(13,2).getValue();
var ownership = sheetTab.getRange(15,2).getValue();
//column headers
var data = sheetTab.getRange(2,4,1,23).getValues();
for (i in data) {
var row = data[i];
var column1 = row[0];
var column2 = row[1];
var column3 = row[2];
var column4 = row[3];
var column5 = row[4];
var column6 = row[5];
var column7 = row[6];
var column8 = row[7];
var column9 = row[8];
var column10 = row[9];
var column11 = row[10];
var column12 = row[11];
var column13 = row[12];
var column14 = row[13];
var column15 = row[14];
var column16 = row[15];
var column17 = row[16];
var column18 = row[17];
var column19 = row[18];
var column20 = row[19];
var column21 = row[20];
var column22 = row[21];
var column23 = row[22];
//SQL statement build & write
var test = conn.createStatement();
if(column2 == '') {var col2 = ''} else {var col2 =',' + column2}
if(column3 == '') {var col3 = ''} else {var col3 =',' + column3}
if(column4 == '') {var col4 = ''} else {var col4 =',' + column4}
if(column5 == '') {var col5 = ''} else {var col5 =',' + column5}
if(column6 == '') {var col6 = ''} else {var col6 =',' + column6}
if(column7 == '') {var col7 = ''} else {var col7 =',' + column7}
if(column8 == '') {var col8 = ''} else {var col8 =',' + column8}
if(column9 == '') {var col9 = ''} else {var col9 =',' + column9}
if(column10 == '') {var col10 = ''} else {var col10 =',' + column10}
if(column11 == '') {var col11 = ''} else {var col11 =',' + column11}
if(column12 == '') {var col12 = ''} else {var col12 =',' + column12}
if(column13 == '') {var col13 = ''} else {var col13 =',' + column13}
if(column14 == '') {var col14 = ''} else {var col14 =',' + column14}
if(column15 == '') {var col15 = ''} else {var col15 =',' + column15}
if(column16 == '') {var col16 = ''} else {var col16 =',' + column16}
if(column17 == '') {var col17 = ''} else {var col17 =',' + column17}
if(column18 == '') {var col18 = ''} else {var col18 =',' + column18}
if(column19 == '') {var col19 = ''} else {var col19 =',' + column19}
if(column20 == '') {var col20 = ''} else {var col20 =',' + column20}
if(column21 == '') {var col21 = ''} else {var col21 =',' + column21}
if(column22 == '') {var col22 = ''} else {var col22 =',' + column22}
if(column23 == '') {var col23 = ''} else {var col23 =',' + column23}
//Define Conditional
var conditional =
sheet.getSheetByName('Config').getRange(2,22).getValue();
Logger.log(conditional);
//SQL statement build & write
var query = 'SELECT '+ column1 + col2 + col3 + col4 + col5 + col6 + col7 + col8 + col9 + col10 + col11 + col12 + col13 + col14 + col15 + col16 + col17 + col18 + col19 + col20 + col21 + col22 + ' FROM unit '+conditional
var sql = conn.createStatement();
var results = sql.executeQuery(query);
var numCols = results.getMetaData().getColumnCount();
//starting cell
var cell = sheetTab.getRange(2,4);
var numCols = results.getMetaData().getColumnCount();
var numRows = sheetTab.getLastRow();
var headers;
var row = 0;
//clear current dataset
sheetTab.getRange(3,4,numRows, numCols).clearContent();
//write sql statement data into sheet
for(var i = 1; i <= numCols; i++){
headers = results.getMetaData().getColumnName(i);
cell.offset(row, i-1).setValue(headers);
}
while (results.next()) {
var rowString = '';
for (var col = 0; col < numCols; col++) {
rowString += results.getString(col + 1) + '\t';
cell.offset(row +1, col).setValue(results.getString(col +1 ));
}
row++
Logger.log(rowString)
}
}
results.close();
test.close();
}
“ col2”,“ col3”等在此上方定义,并且已经在工作表中定义了列标题。
我读到您不能在GAS中的一行上运行多个SQL语句,并且该查询将需要分解然后再次结合在一起。
答案 0 :(得分:0)
您可以使用String.split()
>>> import napalm_star_wars
>>>
>>> sw = napalm_star_wars.napalm_star_wars()
>>>
>>> obi = sw.universe.individual.add("Obi-Wan Kenobi")
>>> obi.affiliation = "REBEL_ALLIANCE"
>>> obi.age = 57
>>>
>>> luke = sw.universe.individual.add("Luke Skywalker")
>>> luke.affiliation = "REBEL_ALLIANCE"
>>> luke.age = 19