跳到主要内容

从附件列提取 URL

信息

从附件中提取可以直接访问的网络链接

实际效果

attachment-convert-to-url

源代码

const datasheet = await space.getActiveDatasheetAsync();

const attachmentField = await input.fieldAsync("请选择附件所在列:", datasheet);
const urlField = await input.fieldAsync("请选择 URL 生成列:", datasheet);

const isCover = await input.textAsync('是否覆盖已有的 URL(是/否):');

// 如果「是否覆盖已有的 URL」输入的值不是「是」或「否」,则提示
if (isCover != "是" && isCover != "否"){
output.text('请输入正确的值!!!');
}else{
const urlFieldId = urlField.id;
const attachmentFieldId = attachmentField.id;

const records = await datasheet.getRecordsAsync();

for (let record of records) {
const recordId = record.id;
const attachmentCellValue = record.getCellValue(attachmentFieldId);
const urlCellValue = record.getCellValueString(urlFieldId);
//为空的数据不属于要转成 URL 的内容,所以跳过
if (attachmentCellValue == null) continue;
if (isCover == "否" && urlCellValue != null) continue;
// 获取附件的 url 值
const url = attachmentCellValue[0].url;
datasheet.updateRecordAsync(recordId, {[urlFieldId]: url});
}

output.text('已将附件转成 URL!!!');
}