Я пытаюсь создать скрипт, который будет идентифицировать заголовок столбца, который соответствует введенной переменной, а затем удалит указанный столбец.У меня есть скрипт для работы, но только если переменная жестко запрограммирована.Я надеюсь, что скрипт будет работать с использованием переменной, переданной из другой функции.
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>
Когда я тестирую скрипт, используя жестко запрограммированное значение «Test», я могу получить совпадение, и соответствующий столбец удаляется успешно.Однако, когда я изменяю var name = 'Test';
на var name = product;
, он не находит соответствия и ничего не происходит.
FWIW Переменная product передается с боковой панели HTML.Я уверен, что это что-то простое и очевидное, и я начинаю думать, что это как-то связано с форматом передаваемой переменной и выводом values[row][col]
, который отличается.Я также пытался преобразовать оба значения в строки, но это не имело значения.
Последние 2 дня я пытался найти возможные исправления, но пока ничего не придумал.Я просто не уверен, как это исправить.Любая помощь с благодарностью!