修改tinyurl Google脚本功能

发布于 2025-02-14 00:13:31 字数 1341 浏览 3 评论 0原文

我一直在使用此功能来缩短链接(如果长度超过200,则在同一单元格中的新tinyurl链接上复制和粘贴)。但是我想修改它以获取活动单元格或活动范围,而不是从UI提示响应中进行输入范围。在这种情况下,我可能也不需要(x.length> 200)条件。我试图研究一些我可以实施的解决方案,但它对于初学者的技能和对原始代码的理解而无法修改它太复杂了。我可以为此做一个简单的修复吗?

function tinyUrl() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi();
  var final = [];
  var response = ui.prompt('Enter range:');
  if(response.getSelectedButton() == ui.Button.Ok) {
    try{
      var values = [].concat.apply([], ss.getRange(response.getResponseText()).getValues()).filter(String);
      SpreadsheetApp.getActiveSpreadsheet().toast('Processing range: '+ss.getRange(response.getResponseText()).getA1Notation(),'Task Started');
      values.forEach(x => {
        if(x.length > 200) {
          final.push(["IMPORTDATA(concatenate(\"http:// tiny url.com/api-create.php?url="+x+"\"),\"\")"]);
        }else{
          final.push(["=HYPERLINK(\""+x+"\")"]);
        }
      })
    }catch(e){
      SpreadsheetApp.getUi().alert(response.getResponseText()+' is not valid range!');
    }
  }else{
    return;
  }
  var r = response.getResponseText().split(":");
  if(r[0].length=1 &&r[1].length == 1){
    ss.getRange(r[0]+"1:"+r[0]+final.lenght).setFormulas(final);
  }else{
    ss.getRange(r[0]+":"+r[0].slice(0,1)+final.length).setFormulas(final);
  }
}

I've been using this function to shorten links (Get range, if length over 200, copy and paste over new tinyurl link in same cell). But I wanna modify it to take active cell or active range instead of input range from UI prompt response. In this case I probably wouldn't need the if(x.length > 200) condition either. I've tried to research for some solutions that I could implement but its too complex for my beginner skills and understanding of original code to modify. Is there an easy fix I could do to it?

function tinyUrl() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi();
  var final = [];
  var response = ui.prompt('Enter range:');
  if(response.getSelectedButton() == ui.Button.Ok) {
    try{
      var values = [].concat.apply([], ss.getRange(response.getResponseText()).getValues()).filter(String);
      SpreadsheetApp.getActiveSpreadsheet().toast('Processing range: '+ss.getRange(response.getResponseText()).getA1Notation(),'Task Started');
      values.forEach(x => {
        if(x.length > 200) {
          final.push(["IMPORTDATA(concatenate(\"http:// tiny url.com/api-create.php?url="+x+"\"),\"\")"]);
        }else{
          final.push(["=HYPERLINK(\""+x+"\")"]);
        }
      })
    }catch(e){
      SpreadsheetApp.getUi().alert(response.getResponseText()+' is not valid range!');
    }
  }else{
    return;
  }
  var r = response.getResponseText().split(":");
  if(r[0].length=1 &&r[1].length == 1){
    ss.getRange(r[0]+"1:"+r[0]+final.lenght).setFormulas(final);
  }else{
    ss.getRange(r[0]+":"+r[0].slice(0,1)+final.length).setFormulas(final);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2025-02-21 00:13:31

确切的解决方案取决于您的实际应用程序。我认为您的问题是如何检测在哪里应用您的自定义功能。但是,让我们退后一步。

有两种方法可以与表格中的现有数据进行交互,并通过appsscript进行自定义函数。

一个是,您可以直接调用表格中的范围来调用自定义功能。如果输入范围是单个单元格,则直接读取数据。如果输入范围跨越单个单元格,则数据将读取为嵌套列表:列列的列列表。例如,a1:b2将被读取为[[A1,B1],[A2,B2]]

因此,例如,您可以随时调用自定义功能tinyurl()在表中输入url的任何地方,并让自定义功能决定何时缩短它。由于您的自定义功能被称为现场,因此没有检测问题。 UI提示不需要。

不利的一面是,如果您在太多地方调用自定义功能,将会延迟获得结果。而且我认为结果并不始终存储在表格中。 (即

。 Apps-script/reference/reveradysheet/范围“ rel =“ nofollow noreferrer”> range 类是您正在使用的类。您可以不使用UI提示,而可以添加oneDit() 触发到您的自定义功能,让您的函数要么通过vendrysheetapp.getactive()。getActiverange()检查当前选择的单元格或扫描您打算存储用户输入的表格上的URL。

使用OnEdit()触发器的缺点是UI滞后。依靠积极选择的范围也可能是有问题的。但是,如果您在整个纸上进行扫描,那么,您必须这样做。最后,您可以用表永久存储所得的URL。因此,在最初的滞后之后,将来您将不会延迟。

在此路线中,您可能会发现 /code> getlastColumn()在表格和 getNextDataCell(getNextDataCell)()如果选择处理整个表,则可以使用onopen()触发器。

我希望将所有URL存储在一个地方,并使用第一选项。因此,自定义功能仅调用一次,这对于最大程度地减少延迟很有用。纸的其他部分可以参考集中式范围内的单元。

确切的解决方案取决于您的实际应用程序。

The exact solution depends on your actual application. I think your question is how to detect where to apply your custom function. But let's take a step back.

There are two ways to interact with existing data in sheet and a custom function via AppsScript.

One is that you can directly call your custom function with ranges in the sheet. If the input range is a single cell, the data is read directly. If the input range spans more than a single cell, the data is read as nested lists: a list of columns which are lists of rows. For example, A1:B2 will be read as [[A1, B1], [A2, B2]].

So, for example, you can always call your custom function tinyurl() wherever you input url in your sheet and let your custom function decide when to shorten it. Since your custom function is called in-place, there is no detection issue; UI prompt is not required.

The downside is that if you call your custom function in too many places, there will be delay in getting results. And I don't think the results are always stored with the sheet. (ie. when you open the sheet again, all cells with tinyurl() may refresh and cause delay.)

Second method is via the Range Class which is what you are using. Instead of using UI prompt though, you can add onEdit() trigger to your custom function and let your function either check for currently selected cell via SpreadsheetApp.getActive().getActiveRange() or scan for urls over the sheet where you intend for user inputs to be stored.

The downside of using onEdit() trigger is the UI lag. Relying on actively selected range can also be problematic. Yet if you scan over the whole sheet, well, you have to do that. At the end though, you get to store the resultant URLs permanently with the sheet. So after the initial lag, you won't have delays in the future.

In this route, you may find getLastRow(), getLastColumn() in Sheet and getNextDataCell() in Range convenient. If you choose to process the whole sheet, you may instead use the onOpen() trigger.

I would prefer to store all URLs in one place and use the 1st option. Thus, the custom function is only called once and that's useful for minimizing delay. Other parts of the sheet can reference cells in that centralized range.

The exact solution depends on your actual application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文