【发布时间】:2021-08-24 09:36:12
【问题描述】:
我正在寻找一些关于我正在尝试编写的 Google Apps 脚本的指导。
我有一个包含数千个链接的 Google 表格。我需要检查链接是否仍然“活动”,如果没有将它们标记为“关闭”。为了节省对每一行的迭代,我首先过滤行以获取在row[9] 中设置了“活动”的所有链接。然后我使用 fetch 来获取响应代码,并根据该代码将 row[9] 的值更新为“Still Active”或“Closed”
我知道我可以在任何其他情况下使用这个概念:
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sites").getRange().setValue();
但是,我发现很难将它与我需要的 filteredRows 结合使用。
表格看起来有点像这样:
| date | name | link | status |
|------------|--------- |---------------------------|--------|
| 2021-08-24 | Google | https://www.google.com/ | Active |
| 2021-08-20 | Facebook | https://www.facebook.com/ | Active |
| 2021-08-18 | Twitter | https://www.twitter.com/ | Closed |
当前脚本:
function getStatus() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sites");
var rows = sheet.getDataRange().getValues();
var filteredRows = rows.filter(function (row) {
var status = row[9];
if (status === "Active") {
return row
}
});
filteredRows.forEach(function(row){
var link = row[3];
var status = row[9];
var options = {
'muteHttpExceptions': true,
'followRedirects': false,
'validateHttpsCertificates': false
};
var response = UrlFetchApp.fetch(link, options);
if (status === "Active") {
if (response.getResponseCode() == 200) {
row.status.setValue("Still Active!");
}
else {
row.status.setValue("Closed!");
}
}
})
}
【问题讨论】:
-
你应该尝试使用像 sheetfu 应用脚本这样的库,这样事情会更干净:github.com/socialpoint-labs/sheetfu-apps-script。这样你的用例就很简单了。
-
你能分享你的工作表吗?
-
@RafaGuillermo 无法共享工作表,但提供了布局示例。希望这会有所帮助
标签: google-apps-script google-sheets