我正在尝试创建一个脚本,该脚本将识别与输入的变量匹配的列标题,然后删除该列。我有脚本可以工作,但是只有在变量是硬编码的情况下才可以。我希望脚本使用从另一个函数传递来的变量来工作。
Code.gs
//Delete identified Product Line Priority and Roadmap tabs
function delProduct(product) {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for(var i =0;i<sheets.length;i++){
Logger.log(i);
if(sheets[i].getName().match(product) != null){
ss.deleteSheet(sheets[i]);
}
}
getColName(product);
}
// Get columns to Delete in Resources tab
function getColName(product) {
var resources = ss.getSheetByName("Resources");
var name = product;
delResources(resources, name);
}
// Admin delete identified Product Line in Resources tab
function delResources(sheet, name) {
var range = sheet.getRange(1,1,1,sheet.getLastColumn());
var values = range.getValues();
for (var row in values) {
for (var col in values[row]) {
if (values[row][col] == name) {
sheet.deleteColumn(parseInt(col)+1);
}
}
}
}
// send list of names to the sidebar
function getProductList() {
var out = new Array();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i=0;i<sheets.length;i++){
Logger.log(i);
if(sheets[i].getName().match("Priority") != null){
out.push([sheets[i].getName()]);
}
}
var regex = /Priority/gi;
var text = String(out);
var splitData = text.toString().replace(regex, "");
var myData = splitData.split(",");
var displayData = myData.sort();
return displayData
}
HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="jquery-1.11.1.min.js"></script>
<script src="jquery-ui.min.js"></script>
<script src="jquery.select-to-autocomplete.js"></script>
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<link rel="stylesheet" href="jquery-ui.css">
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<style>
body {
font-family: Arial, Verdana, sans-serif;
font-size: 14px;
}
body2 {
font-family: Arial, Verdana, sans-serif;
font-size: 32px;
}
body3 {
font-family: Arial, Verdana, sans-serif;
font-size: 10px;
color: #C0C0C0
}
body4 {
font-family: Arial, Verdana, sans-serif;
font-size: 11px;
}
ul{
list-style-type: square;
list-style-position: inside;
padding: 0;
}
userInput{
margin: 1px;
padding: 1px;
height: 80px;
resize: none;
}
userInput2{
margin: 1px;
padding: 1px;
height: 300px;
resize: none;
}
.logo {
position: relative;
display: inline;
}
.imtip1 {
position: relative;
bottom: 150px;
left: 0px;
line-height: 80%;
}
.imtip2 {
position: relative;
bottom: 70px;
left: 0px;
line-height: 50%;
}
.text {
position: relative;
bottom: 240px;
left: 0;
color: #000000;
font-size:14pt;
line-height: 0%;
}
.text1 {
position: relative;
bottom: 250px;
left: 10px;
color: #000000;
font-size:11pt;
}
.text2 {
position: relative;
bottom: 250px;
left: 0px;
color: #000000;
font-size:10pt;
}
</style>
</head>
<center class='logo'>
<img src="image.gif" onclick="google.script.run.sideBar()" width="300px" height="260px" class="imtip2" />
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAA1BMVEX///+nxBvIAAAASElEQVR4nO3BgQAAAADDoPlTX+AIVQEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwDcaiAAFXD1ujAAAAAElFTkSuQmCC" width="300px" height="80px" class="imtip1" />
</center>
<center class = "text" align = "center">
<hr style="height: 1px; width: 105%; background-color: #848484" />
</center>
<form target="_top" style="padding: 10px;">
<div class = "text2">
<br><br>
<center>
<span style="color: #ff0000;"><b style="font-size: 20px;">*** CAUTION ***</b></span>
<br><br><br>
<span style="line-height:120%; font-size: 12px;">Select the Product from the list below to submit your request to Product Leadership.</span>
</center>
<br><br><br>
<body style="line-height:80%;">
<center>
<b>Product:</b>
<select id="optionList" name="optionList">
<option disabled selected value="">Select Product...</option>
</select>
</center>
</body>
</div>
<div style="line-height:90%;">
<center class = "text2" >
<br><br><br>
<input class="red" type="button" value="Cancel" onclick="priority()" style="-moz-box-shadow: 2px 2px 2px #ccc; -webkit-box-shadow: 2px 2px 2px #ccc; box-shadow: 2px 2px 2px #ccc; -moz-border-radius:3px; -webkit-border-radius:3px; border-radius:3px;"/>
<input class="green" type="button" value="SUBMIT" onclick="deleteProduct()" style="-moz-box-shadow: 2px 2px 2px #ccc; -webkit-box-shadow: 2px 2px 2px #ccc; box-shadow: 2px 2px 2px #ccc; -moz-border-radius:3px; -webkit-border-radius:3px; border-radius:3px;"/>
</center>
<body4 align="right" style="right: 2%;">
<p>
<a onclick="supEmail()" target="_top">Contact Support</a>
</p>
</body4>
</div>
<script>
function onSuccess(values) {
var select = document.getElementById("optionList");
var options = values; //Two dimensional array
for(var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
}
google.script.run.withSuccessHandler(onSuccess)
.getProductList();
</script>
<script>
function deleteProduct(){
var product = document.getElementById("optionList").value;
google.script.run.delProduct(product);
google.script.run.sideBar();
}
</script>
<script>
function supEmail() {
google.script.run.supEmail();
}
</script>
<script>
function priority() {
google.script.run.addPrioritySidebar();
}
</script>
</form>
</html>
当我使用硬编码的“测试”值测试脚本时,我能够获得匹配项,并且相应的列已成功删除。但是,当我将var name = 'Test';
更改为var name = product;
时,找不到匹配项,什么也没发生。
FWIW product 变量是从HTML侧边栏传递的。我确信这是简单明了的事情,并且我开始认为这与传递变量的格式和values[row][col]
的输出不同有关。我也尝试将两个值都转换为字符串,但这没什么区别。
最近2天,我一直在尝试研究可能的修复程序,但是我还没有提出任何建议。我只是不确定如何解决。任何帮助将不胜感激!
答案 0 :(得分:1)
在定义解决问题的变量时,我在代码中包括了.toString()。trim()。