简体   繁体   English

如何将文本插入另一个页面的输入字段?

[英]How can I insert text into an input field on another page?

I have a list of URLs and some info on a Google Sheet and managed to open the URLs with a Google Apps Script.我在 Google 表格上有一个 URL 列表和一些信息,并设法使用 Google Apps 脚本打开了这些 URL。 On the page that opens, there are 2 input fields and a confirm button.在打开的页面上,有 2 个输入字段和一个确认按钮。 I need the script to get info from the spreadsheet (which I saved as variables) and input them into these fields.我需要脚本从电子表格(我保存为变量)中获取信息并将它们输入到这些字段中。 After that, I need the script to click on the submit button.之后,我需要脚本来点击提交按钮。

I'm not entirely sure this would be the solution, but I figured I would need to get the input field element by id and then insert the variable in it.我不完全确定这是否是解决方案,但我认为我需要通过 id 获取输入字段元素,然后将变量插入其中。 But I wasn't able to do it because getElementById() doesn't work with Google Apps Script.但我无法做到这一点,因为getElementById()不适用于 Google Apps 脚本。

I did quite some digging and only found solutions that used the HTML file on a Google Apps Script.我做了一些挖掘工作,只找到了在 Google Apps 脚本上使用 HTML 文件的解决方案。 However, I don't know if it would help solve this issue and I honestly didn't understand much of it.但是,我不知道它是否有助于解决这个问题,老实说,我不太了解它。 I guess it would only add unnecessary steps since I already managed to open the page on another tab.我想这只会增加不必要的步骤,因为我已经设法在另一个选项卡上打开了该页面。

I have the ID of the first input field.我有第一个输入字段的 ID。 The second field is one "Tab hit" away from the first one and has a dynamic ID, so it won't work when I loop the script to do it with all the URLs.第二个字段与第一个字段相距一个“Tab hit”,并且具有动态 ID,因此当我循环脚本以使用所有 URL 执行此操作时,它将不起作用。 The only property that seems unique to it is the aria-label (I don't know if it helps).似乎唯一的属性是 aria-label (我不知道它是否有帮助)。 The submit button doesn't have an ID but it has a unique class and a type="submit" (I can see it on inspect but not when I open the source code on another page, don't know if there's a difference).提交按钮没有 ID,但它有一个唯一的类和一个 type="submit"(我可以在检查时看到它,但当我在另一个页面上打开源代码时看不到,不知道是否有区别) .

I already managed to open the URLs on another tab and save the info on variables, now I'm stuck on the step that inserts them on the input fields and clicks the submit button.我已经设法在另一个选项卡上打开 URL 并保存变量信息,现在我被困在将它们插入输入字段并单击提交按钮的步骤上。

Here's my code so far:到目前为止,这是我的代码:

function fulfill() {
  var rangeValues = searchRange.getValues();
  var orderId = rangeValues[604][1];
  var trackingNumber = rangeValues[604][3];
  var shippingCompany = rangeValues[604][4];
  var url = "https://someshopifystore.myshopify.com/admin/orders/" + orderId + "/fulfill_and_ship?fulfill_only=true&fulfillment_service_handle=manual&location_id=2085355564&requires_shipping=true";
  var html = "<script>window.open('" + url + "');google.script.host.close();</script>";
  var userInterface = HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Tab');
}

OBS1: I need to solve it through Google Sheets and Google Apps Script because it's part of a wider solution. OBS1:我需要通过 Google 表格和 Google Apps 脚本来解决它,因为它是更广泛解决方案的一部分。

OBS2: The page that opens is a fulfillment page from Shopify. OBS2:打开的页面是 Shopify 的履行页面。

If you do want to solve to implement your functionality with Google Apps Script, here some basic key points:如果您确实想解决以使用 Google Apps 脚本实现您的功能,这里有一些基本要点:

  • Have a look at the reference and guides for the usage of Apps Script HTML service.查看 Apps Script HTML 服务的使用参考指南
  • Bounding an HTML file to your script allows you to embed Javascript code within the <script> </script> part of a HTML file.将 HTML 文件绑定到脚本允许您将 Javascript 代码嵌入 HTML 文件的<script> </script>部分。
  • This is where you can use functions that are not supported by Apps Script (eg getElementById() ).在这里您可以使用 Apps 脚本不支持的函数(例如getElementById() )。
  • To call an Apps Script function from the JS part, you can use the google.script.run method .要从 JS 部分调用 Apps Script 函数,您可以使用google.script.run 方法
  • To set values to HTML input fields manually, you need to access those elements, eg by their ID or name.要手动设置 HTML 输入字段的值,您需要访问这些元素,例如通过它们的 ID 或名称。 You need to inspect the source code of your URL to see how those input fields have been defined and can be identified.您需要检查 URL 的源代码,以了解这些输入字段是如何定义和识别的。 Subsequently you have to trigger a submit event.随后,您必须触发提交事件。
  • If you use the Shopify API instead, you can bypass the creation of HTML file with JS script.如果您改用 Shopify API,则可以绕过使用 JS 脚本创建 HTML 文件。 All you have to do is to chose the right API (eg RESTful API to create an order), copy the JSON request (adapting it to your needs) and post the request with UrlFetchApp.fetch() - with the order request as payload within the request options.您所要做的就是选择正确的 API(例如创建订单的 RESTful API),复制 JSON 请求(根据您的需要调整它)并使用UrlFetchApp.fetch()发布请求 - 将订单请求作为payload请求选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从输入字段获取值并将其值插入另一个输入? - How can I get the value from input field and insert its value into another input? 如何通过QWebEngine在网页的输入文本字段中填充文本(来自C ++)? - How can I fill in text (from C++) in an input text field on a web page through QWebEngine? MooTools:如何用文本插入替换现有的字段文本? - MooTools: How can I replace existing field text with text insert? 如何使用父页面上的输入字段搜索 iframe 内容(文本)? - How can I search iframe content (text) using an input field that is on the parent page? 如何在输入表单字段中插入当前日期作为值 - How can i insert current date as a value in an input form field 如何将输入文本字段替换为跨度文本? - how can I replace input text field into text in span? 我如何将文本更改为单击文本本身的输入字段 - How can I change text to input field clicking on the text itself 如何使用输入字段将文本插入表格 - How to insert text into table with input field 如何插入可以将用户输入作为文本的选项 - How do I insert an option that can take user input as text 如何以与更改另一个输入字段相同的数量来更改一个输入字段? - How can I change one input field in the same amount I change another input field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM