gpt4 book ai didi

javascript - 按名称查找列并删除列

转载 作者:行者123 更新时间:2023-12-03 00:53:42 25 4
gpt4 key购买 nike

我正在尝试创建一个脚本,该脚本将识别与输入的变量匹配的列标题,然后删除所述列。我已经让脚本可以工作,但前提是变量是硬编码的。我希望让脚本使用从另一个函数传递的变量来工作。

代码.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;"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<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 天我一直在尝试研究可能的修复方法,但尚未提出任何建议。我只是不确定如何修复。非常感谢任何帮助!

最佳答案

我在定义解决问题的 var 时在代码中包含了 .toString().trim() 。

关于javascript - 按名称查找列并删除列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52941302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com