Skip to content

Instantly share code, notes, and snippets.

@cddouglass
Created September 24, 2023 01:56
Show Gist options
  • Select an option

  • Save cddouglass/2fc0ea311d7b6b10172769ac02688488 to your computer and use it in GitHub Desktop.

Select an option

Save cddouglass/2fc0ea311d7b6b10172769ac02688488 to your computer and use it in GitHub Desktop.
let settings = input.config({ title: 'Find and replace', description: `This script will find and replace all text matches for a text-based field you pick. You will be able to see all matches before replacing them.`, items: [ input.config.table('table', { label: 'Table' }), input.config.field('field', { parentTable: 'table', label: 'Field' }), ],});
let { table, field } = settings;output.text(`Finding and replacing in the ${field.name} field of ${table.name}.`);
let findText = await input.textAsync('Enter text to find:');let replaceText = await input.textAsync('Enter to replace matches with:');
// Load all of the records in the tablelet result = await table.selectRecordsAsync();
// Find every record we need to updatelet replacements = [];for (let record of result.records) { let originalValue = record.getCellValue(field);
// Skip non-string records if (typeof originalValue !== 'string') { continue; }
// Skip records which don't have the value set, so the value is null if (!originalValue) { continue; }
let newValue = originalValue.replace(findText, replaceText);
if (originalValue !== newValue) { replacements.push({ record, before: originalValue, after: newValue, }); }}if (!replacements.length) { output.text('No replacements found');} else { output.markdown('## Replacements'); output.table(replacements);
let shouldReplace = await input.buttonsAsync('Are you sure you want to save these changes?', [ { label: 'Save', variant: 'danger' }, { label: 'Cancel' }, ]);
if (shouldReplace === 'Save') { // Update the records let updates = replacements.map((replacement) => ({ id: replacement.record.id, fields: { [field.id]: replacement.after, }, }));
// Only up to 50 updates are allowed at one time, so do it in batches while (updates.length > 0) { await table.updateRecordsAsync(updates.slice(0, 50)); updates = updates.slice(50); } }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment