how to edit blank key values in a batch

edited January 4, 2021
var fieldName = "language";
var oldValue = " ";
var newValue = "en";

188BET靠谱吗var fieldID = Zotero.ItemFields.getID(fieldName);
188BET靠谱吗var s = new Zotero.Search();
188BET靠谱吗s.libraryID = ZoteroPane.getSelectedLibraryID();
s.addCondition(fieldName, 'is', oldValue);
var ids = await s.search();
if (!ids.length) {
return "No items found";
}
188BET靠谱吗await Zotero.DB.executeTransaction(async function () {
for (let id of ids) {
188BET靠谱吗let item = await Zotero.Items.getAsync(id);
188BET靠谱吗let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
item.setField(mappedFieldID ?mappedFieldID : fieldID, newValue);
await item.save();
}
});
return ids.length + " item(s) updated";
  • I want to replace blank values for language with en, however, the above javascript does not work
  • can anyone help me out?Thanks very much!
  • Enter a blank string for oldValue, rather than a space.

    "" not " "
  • there is someting wrong using "" not " "
    SyntaxError: unexpected token: identifier
  • var fieldName = "language";
    var oldValue = "" not " ";
    var newValue = "en";

    188BET靠谱吗var fieldID = Zotero.ItemFields.getID(fieldName);
    188BET靠谱吗var s = new Zotero.Search();
    188BET靠谱吗s.libraryID = ZoteroPane.getSelectedLibraryID();
    s.addCondition(fieldName, 'is', oldValue);
    var ids = await s.search();
    if (!ids.length) {
    return "No items found";
    }
    188BET靠谱吗await Zotero.DB.executeTransaction(async function () {
    for (let id of ids) {
    188BET靠谱吗let item = await Zotero.Items.getAsync(id);
    188BET靠谱吗let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
    item.setField(mappedFieldID ?mappedFieldID : fieldID, newValue);
    await item.save();
    }
    });
    return ids.length + " item(s) updated";

    这是我所使用的:
  • @fredericky: I would suggest to use the Zutilo add-on for this, see here.

    Copy {"itemType": "book", "language": "en"}to the clipboard and use Zutilo's "Paste into empty item fields".The "itemType" name/value pair needs to be kept for technical reasons, its value is irrelevant for this function.

    Also, remember to 188BET靠谱吗back up your Zotero data188BET靠谱吗, in particular your 'zotero.sqlite' database.Running code that you are not sure about can have unwanted results.
  • 188BET靠谱吗You can also create a new empty Zotero item and only fill in "en" for the Language.Then use Zutilo's "Copy item fields" and paste that into empty fields for your selected items.
  • Thanks!This is quite useful!
Sign Inor Registerto comment.